Last active
February 23, 2021 11:34
-
-
Save juban/9722de585f210e1f702e4ee82c67e892 to your computer and use it in GitHub Desktop.
Craft CMS 3 - Add a Field to a section fields layout programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// To put somewhere in a module, plugin or content migration | |
// Get the entry type to add the new field to | |
$entryType = Craft::$app->getSections()->getSectionByHandle('someSectionHandle')->entryTypes[0]; | |
$field = Craft::$app->getFields()->getFieldByHandle('someFieldHandle'); | |
// Get current fieldLayout | |
$fieldLayout = $entryType->getFieldLayout(); | |
$amendedFields = ArrayHelper::merge([$field], $fieldLayout->getFields()); | |
$fieldLayout->setFields($amendedFields); | |
$fieldLayout->getTabs()[0]->setFields($amendedFields); | |
// Change entryType fieldLayout | |
$entryType->setFieldLayout($fieldLayout); | |
// Save entryType configuration | |
Craft::$app->getSections()->saveEntryType($entryType); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment