Answers.Body: <p>If you want to add a tab dynamically to a field layout as it's being displayed, <code>FieldLayout::EVENT_DEFINE_NATIVE_FIELDS</code> is not the best event to use. This one would be appropriate if you wanted to add an additional native field that developers could add to the element's field layouts manually. Instead, use <code>FieldLayout::EVENT_CREATE_FORM</code> to add a <code>FieldLayoutTab</code> when the element form is being created. Inside a module:</p>
  <pre class="lang-php prettyprint-override"><code>// class imports
  use craft\fieldlayoutelements\TextField;
  use craft\models\FieldLayout;
  use craft\models\FieldLayoutTab;
  use yii\base\Event;
  
  // init()
  Event::on(
      FieldLayout::class,
      FieldLayout::EVENT_CREATE_FORM,
      function (CreateFieldLayoutFormEvent $e) {
          $element = $e-&gt;element;
          $e-&gt;tabs[] = new FieldLayoutTab([
              'name' =&gt; Craft::t('app', 'My custom tab'),
              'layout' =&gt; $e-&gt;sender,
              'elements' =&gt; [
                  new TextField([
                      'label' =&gt; 'My Description',
                      'attribute' =&gt; 'description',
                      'instructions' =&gt; 'Enter a description.',
                  ])
              ],
          ]);
      }
  );
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-10T07:54:50.560Z
Answers.LastActivityDate: 2022-08-10T07:54:50.560Z
Answers.UserDisplayName: MoritzLost
Body: <p>I'm trying to add a new Tab in entry elements from my module using events.</p>
  <pre><code>Event::on(
              FieldLayout::class,
              FieldLayout::EVENT_DEFINE_NATIVE_FIELDS,
              static function(DefineFieldLayoutFieldsEvent $event) {
                  /** @var FieldLayout $fieldLayout */
                  $fieldLayout = $event-&gt;sender;
  
  
                  $tabs = $fieldLayout-&gt;getTabs();
  
                  $layoutElements = [
                      new TextField([
                          'label' =&gt; 'My Description',
                          'attribute' =&gt; 'description',
                          'mandatory' =&gt; true,
                          'instructions' =&gt; 'Enter a description.',
                      ])
                  ];
  
                  $newTab = new FieldLayoutTab();
                  $newTab-&gt;name = 'Audit';
                  $newTab-&gt;layoutId = $fieldLayout-&gt;id;
                  $newTab-&gt;setLayout($fieldLayout);
                  $newTab-&gt;setElements($layoutElements);
  
                  $fieldLayout-&gt;setTabs([...$tabs, $newTab]);
  
              }
          );
  </code></pre>
  <p>What is the correct way to add a new tab in entry ?</p>
  
Tags: <events><hook>
Title: Craft 4: How to programmatically attach a new tab in elements tab?
ThreadText: # Craft 4: How to programmatically attach a new tab in elements tab?
  
  (Asked by: username_0 on 2022-08-10)
  
  I'm trying to add a new Tab in entry elements from my module using events.
  
  ```
  Event::on(
              FieldLayout::class,
              FieldLayout::EVENT_DEFINE_NATIVE_FIELDS,
              static function(DefineFieldLayoutFieldsEvent $event) {
                  /** @var FieldLayout $fieldLayout */
                  $fieldLayout = $event->sender;
  
                  $tabs = $fieldLayout->getTabs();
  
                  $layoutElements = [
                      new TextField([
                          'label' => 'My Description',
                          'attribute' => 'description',
                          'mandatory' => true,
                          'instructions' => 'Enter a description.',
                      ])
                  ];
  
                  $newTab = new FieldLayoutTab();
                  $newTab->name = 'Audit';
                  $newTab->layoutId = $fieldLayout->id;
                  $newTab->setLayout($fieldLayout);
                  $newTab->setElements($layoutElements);
  
                  $fieldLayout->setTabs([...$tabs, $newTab]);
  
              }
          );
  
  ```
  
  What is the correct way to add a new tab in entry ?
  
  **Tags:** events, hook
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-08-10. Score: 1
  
  If you want to add a tab dynamically to a field layout as it's being displayed, `FieldLayout::EVENT_DEFINE_NATIVE_FIELDS` is not the best event to use. This one would be appropriate if you wanted to add an additional native field that developers could add to the element's field layouts manually. Instead, use `FieldLayout::EVENT_CREATE_FORM` to add a `FieldLayoutTab` when the element form is being created. Inside a module:
  
  ```php
  // class imports
  use craft\fieldlayoutelements\TextField;
  use craft\models\FieldLayout;
  use craft\models\FieldLayoutTab;
  use yii\base\Event;
  
  // init()
  Event::on(
      FieldLayout::class,
      FieldLayout::EVENT_CREATE_FORM,
      function (CreateFieldLayoutFormEvent $e) {
          $element = $e->element;
          $e->tabs[] = new FieldLayoutTab([
              'name' => Craft::t('app', 'My custom tab'),
              'layout' => $e->sender,
              'elements' => [
                  new TextField([
                      'label' => 'My Description',
                      'attribute' => 'description',
                      'instructions' => 'Enter a description.',
                  ])
              ],
          ]);
      }
  );
  
  ```
  
  
  ---
  
  