I'm in the process of upgrading a plugin from Craft 3 to Craft 4. When the plugin is installed it creates some fields and attaches them to an entry type. The way it was done in Craft 3 is:

  1. Get the Section
  2. Get the first Entry Type for said Section
  3. Get Field Layout for said Entry Type
  4. Get Tabs for said Field Layout
  5. Assign fields to First Tab.
  6. Assign updated Tab to Layout
  7. Assign Field Layout to Entry Type
  8. Save Entry Type


    $section = Craft::$app->sections->getSectionByHandle(Handles::CHANNEL);
    $entryType = $section->getEntryTypes()[0];
    $layout = $entryType->getFieldLayout();

    $tabs = $layout->getTabs();

    $tabs[0]->setFields(array_merge($tabs[0]->getFields(), $fields));




This fails on Craft 4, because the FieldLayoutTab class no longer has a setFields method (nor a getFields method, which was replaced with a fields method).

What would be the correct way to attach existing Fields to Tab in Craft 4?

1 Answer 1


I just ran into this myself. You can use getElements and setElements instead.

This is for a category though:

    use craft\fieldlayoutelements\CustomField;

    public function safeUp(): bool
        $field = Craft::$app->fields->getFieldByHandle('sortedProducts');
        $catGroup = Craft::$app->categories->getGroupByHandle('artworkCategories');
        $layout = $catGroup->getFieldLayout();
        $tabs = $layout->getTabs();

        foreach ($tabs[0]->elements as $i => $element) {
            if ($element instanceof CustomField && $element->getField()->handle === 'sortedProducts') {
                return true;

        $newElement = [
            'type' => CustomField::class,
            'fieldUid' => $field->uid,
            'required' => false,

        $tabs[0]->setElements(array_merge($tabs[0]->getElements(), [$newElement]));

        return true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.