Im creating my own migrations to better handle adding/removing fields on all environments.
Is the following safeDown()
function the correct way to remove a field from a fieldlayout? It works but im open if their is a better way to archive this.
public function safeDown()
{
$tabStructure = [];
$cartDestinationUrl = Craft::$app->fields->getFieldByHandle('cartDestinationUrl');
$headerGlobalSet = Craft::$app->globals->getSetByHandle('header');
$tabs = $headerGlobalSet->getFieldLayout()->getTabs();
foreach ($tabs as $tab) {
$fields = $tab->getFields();
$fieldIds = [];
foreach ($fields as $field) {
$fieldId = $field->id;
if ($fieldId !== $cartDestinationUrl->id) {
$fieldIds[] = $fieldId;
}
}
$tabStructure[$tab->name] = $fieldIds;
}
//remove empty tabs from layout
$tabStructure = array_filter($tabStructure);
$globalSetLayout = Craft::$app->fields->assembleLayout($tabStructure);
$headerGlobalSet->setFieldLayout($globalSetLayout);
return (Craft::$app->globals->saveSet($headerGlobalSet));
}