<p>I eventually figured this out. Here's the code that's working for me:</p>

<pre><code>$newFields = array(
  0 =&gt; '12', // $field-&gt;fieldId
  1 =&gt; '15'  // $field-&gt;fieldId
);

// First we get all sections and loop through them
$allSections = craft()-&gt;sections-&gt;getAllSections();
foreach ($allSections as $section)
{
  // Then we get all EntryTypes of each section and loop through those
  $sectionEntryTypes = $section-&gt;getEntryTypes();
  foreach ($sectionEntryTypes as $entryType) 
  {
    // Grab the current layout and tabs
    $currentLayout = $entryType-&gt;getFieldLayout();
    $currentTabs = $currentLayout-&gt;getTabs();

    // This will the array passed to assembleLayout
    $postedFieldLayout = array();

    // Loop through each tab and build our array
    foreach ($currentTabs as $tab) {
      $fields = $tab-&gt;getFields();
      foreach ($fields as $field) {
        $postedFieldLayout[$tab-&gt;name][] = $field-&gt;fieldId;
      }
    }

    // Append the new fields we want to show to that array
    $postedFieldLayout['newFields'] = $this-&gt;newFields;
    $requiredFields = array();

    // Assemble the layout
    $fieldLayout = craft()-&gt;fields-&gt;assembleLayout($postedFieldLayout, $requiredFields);
    $fieldLayout-&gt;type = ElementType::Entry;
    $entryType-&gt;setFieldLayout($fieldLayout);

    // Save it
    if (craft()-&gt;sections-&gt;saveEntryType($entryType))
    {
      Craft::log('Successfully appended fields.');
    }
    else 
    {
      Craft::log('Could not append the fields.', LogLevel::Warning);
    }
  }
}
</code></pre>

CC BY-SA 3.0
2015-10-22T18:12:44.997Z
2015-10-22T18:12:44.997Z
brianjhanson
<p>I'm trying to create a plugin that adds a tab full of fields to entry types but I can't seem to get it working. I tried <a href="https://buildwithcraft.com/classreference/services/FieldsService#assembleLayout-detail" rel="nofollow"><code>assembleLayout</code></a> but that replaced all the fields with my new fields rather than adding a new tab to them. </p>

<p>I'm trying different combinations of <code>FieldLayoutTabModel-&gt;setFields()</code> but that throws an error about a <code>fieldId</code> property which leads me to believe I don't quite have the data types right. Any help would be appreciated.</p>

<field><entry-type>
How can I programmatically add a tab to entry type?
# How can I programmatically add a tab to entry type?

(Asked by: username_0 on 2015-09-26)

I'm trying to create a plugin that adds a tab full of fields to entry types but I can't seem to get it working. I tried [`assembleLayout`](https://buildwithcraft.com/classreference/services/FieldsService#assembleLayout-detail) but that replaced all the fields with my new fields rather than adding a new tab to them.

I'm trying different combinations of `FieldLayoutTabModel->setFields()` but that throws an error about a `fieldId` property which leads me to believe I don't quite have the data types right. Any help would be appreciated.

**Tags:** field, entry-type


---

## 1 answers

### Answer by username_0 on 2015-10-22. Score: 7 | Accepted answer

I eventually figured this out. Here's the code that's working for me:
$newFields = array(
  0 => '12', // $field->fieldId
  1 => '15'  // $field->fieldId
);

// First we get all sections and loop through them
$allSections = craft()->sections->getAllSections();
foreach ($allSections as $section)
{
  // Then we get all EntryTypes of each section and loop through those
  $sectionEntryTypes = $section->getEntryTypes();
  foreach ($sectionEntryTypes as $entryType) 
  {
    // Grab the current layout and tabs
    $currentLayout = $entryType->getFieldLayout();
    $currentTabs = $currentLayout->getTabs();

    // This will the array passed to assembleLayout
    $postedFieldLayout = array();

    // Loop through each tab and build our array
    foreach ($currentTabs as $tab) {
      $fields = $tab->getFields();
      foreach ($fields as $field) {
        $postedFieldLayout[$tab->name][] = $field->fieldId;
      }
    }

    // Append the new fields we want to show to that array
    $postedFieldLayout['newFields'] = $this->newFields;
    $requiredFields = array();

    // Assemble the layout
    $fieldLayout = craft()->fields->assembleLayout($postedFieldLayout, $requiredFields);
    $fieldLayout->type = ElementType::Entry;
    $entryType->setFieldLayout($fieldLayout);

    // Save it
    if (craft()->sections->saveEntryType($entryType))
    {
      Craft::log('Successfully appended fields.');
    }
    else 
    {
      Craft::log('Could not append the fields.', LogLevel::Warning);
    }
  }
}


---

