<p>I just ran into this myself. You can use <code>getElements</code> and <code>setElements</code> instead.</p>
<p>This is for a category though:</p>
<pre class="lang-php prettyprint-override"><code>    use craft\fieldlayoutelements\CustomField;

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

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

        $newElement = [
            'type' =&gt; CustomField::class,
            'fieldUid' =&gt; $field-&gt;uid,
            'required' =&gt; false,
        ];

        $tabs[0]-&gt;setElements(array_merge($tabs[0]-&gt;getElements(), [$newElement]));
        $layout-&gt;setTabs($tabs);
        Craft::$app-&gt;fields-&gt;saveLayout($layout);

        return true;
    }

</code></pre>

CC BY-SA 4.0
2022-06-10T09:18:06.550Z
2022-06-10T09:18:06.550Z
twined
<p>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:</p>
<ol>
<li>Get the Section</li>
<li>Get the first Entry Type for said Section</li>
<li>Get Field Layout for said Entry Type</li>
<li>Get Tabs for said Field Layout</li>
<li>Assign fields to First Tab.</li>
<li>Assign updated Tab to Layout</li>
<li>Assign Field Layout to Entry Type</li>
<li>Save Entry Type</li>
</ol>
<p>Code:</p>
<pre><code>    $section = Craft::$app-&gt;sections-&gt;getSectionByHandle(Handles::CHANNEL);
    $entryType = $section-&gt;getEntryTypes()[0];
    $layout = $entryType-&gt;getFieldLayout();

    $tabs = $layout-&gt;getTabs();

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

    $layout-&gt;setTabs($tabs);

    $entryType-&gt;setFieldLayout($layout);

    Craft::$app-&gt;sections-&gt;saveEntryType($entryType);
</code></pre>
<p>This fails on Craft 4, because the <code>FieldLayoutTab</code> class no longer has a <code>setFields</code> method (nor a <code>getFields</code> method, which was replaced with a <code>fields</code> method).</p>
<p>What would be the correct way to attach existing Fields to Tab in Craft 4?</p>

<plugin-development><upgrades><craft4>
Craft 4: How to programmatically attach fields to entry type tab
# Craft 4: How to programmatically attach fields to entry type tab

(Asked by: username_0 on 2022-06-09)

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:

Get the Section
Get the first Entry Type for said Section
Get Field Layout for said Entry Type
Get Tabs for said Field Layout
Assign fields to First Tab.
Assign updated Tab to Layout
Assign Field Layout to Entry Type
Save Entry Type

Code:
    $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));

    $layout->setTabs($tabs);

    $entryType->setFieldLayout($layout);

    Craft::$app->sections->saveEntryType($entryType);

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?

**Tags:** plugin-development, upgrades, craft4

---

## 1 answers

### Answer by username_1 on 2022-06-10. Score: 5 | Accepted answer

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]));
        $layout->setTabs($tabs);
        Craft::$app->fields->saveLayout($layout);

        return true;
    }


---

