When I try to remove the field from existing Field Layout of the Entry Type Section it does not remove in the end
public function safeUp() {
$section = Craft::$app->sections->getSectionByHandle('mySection');
$types = $section->getEntryTypes();
$entryType = $types[0];
$fields = $entryType->getFields();
foreach ($fields as $key => $value) {
if ($value->handle == 'someField') {
unset($fields[$key]);
}
}
$fieldLayout = $entryType->getFieldLayout();
$tab = $fieldLayout->getTabs()[0];
$tab->setFields($fields);
$fieldLayout->setTabs([$tab]);
$entryType->setFieldLayout($fieldLayout);
$res = $entryType->setFields($fields);
$resSectionEntrytype = Craft::$app->sections->saveEntryType($entryType);
$resSection = Craft::$app->sections->saveSection($section);
}
As of Craft 3.5, you have to remove the field from the tab鈥檚 $elements array.
use craft\fieldlayoutelements\CustomField;
public function safeUp() {
$section = Craft::$app->sections->getSectionByHandle('mySection');
$types = $section->getEntryTypes();
$entryType = $types[0];
$fieldLayout = $entryType->getFieldLayout();
$tab = $fieldLayout->getTabs()[0];
foreach ($tab->elements as $i => $element) {
if ($element instanceof CustomField && $element->getField()->handle === 'someField') {
unset($tab->elements[$i]);
break;
}
}
Craft::$app->sections->saveEntryType($entryType);
}
Most helpful comment
As of Craft 3.5, you have to remove the field from the tab鈥檚
$elementsarray.