Answers.Body: <p>I had the problems that I used tabs in my plugins. In one of those tabs I save settings in general and in antoher tabs I save the FieldLayout. Here is the adapted method prepSettings()</p>
  
  <pre><code>public function prepSettings($settings)
  {
  
      $postedFieldLayout = craft()-&gt;request-&gt;getParam('fieldLayout') ? craft()-&gt;request-&gt;getParam('fieldLayout') : array();
      $requiredFields = craft()-&gt;request-&gt;getParam('requiredFields') ? craft()-&gt;request-&gt;getParam('requiredFields') : array();
  
      if($postedFieldLayout){
  
          // Delete the old field layout
          craft()-&gt;fields-&gt;deleteLayoutsByType('Foo');
  
          // Save the field layout
          $fieldLayout = craft()-&gt;fields-&gt;assembleLayout($postedFieldLayout, $requiredFields, true);
          $fieldLayout-&gt;type = 'Foo';
          craft()-&gt;fields-&gt;saveLayout($fieldLayout);
      }
  
      return $settings;
  }
  </code></pre>
  
  <p>In the controller you prefer</p>
  
  <pre><code>$fieldLayout = craft()-&gt;fields-&gt;getLayoutByType('Foo');
  // dont foget to add'em as an parameter in renderLayout
  </code></pre>
  
  <p>In the model include</p>
  
  <pre><code>public function getFieldLayout()
  {
      return craft()-&gt;fields-&gt;getLayoutByType('Foo');
  }
  </code></pre>
  
  <p>The content block in my tabs layout</p>
  
  <pre><code>&lt;form method='post' action='' accept-charset='UTF-8' data-saveshortcut data-confirm-unload&gt;
      {{ getCsrfInput() }}
  
      &lt;input type='hidden' name='action' value='plugins/savePluginSettings'&gt;
      &lt;input type='hidden' name='pluginClass' value='FooPluginClass'&gt;
  
      &lt;section id='fields'&gt;
          {% include "_includes/fieldlayoutdesigner" with {
              fieldLayout: fieldLayout
          } only %}
      &lt;/section&gt;
  
       &lt;div class='buttons'&gt;
          &lt;input class='btn submit' type='submit' value='{{ 'Save'|t }}'&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-03T00:32:30.733Z
Answers.LastActivityDate: 2018-02-03T00:32:30.733Z
Answers.UserDisplayName: Simon Franzen
Answers.Body: <p>There are two ways to retrieve field layouts: by ID (<a href="http://buildwithcraft.com/classreference/services/FieldsService#getLayoutById-detail">FieldsService::getLayoutById()</a>) and by element type (<a href="http://buildwithcraft.com/classreference/services/FieldsService#getLayoutByType-detail">FieldsService::getLayoutByType()</a>). The former is useful for element types that manage multiple field layouts and have their own way of storing those field layout IDs (e.g. sections, asset sources, etc.), and the latter is useful for element types that only need to manage a single field layout (e.g. users, or in your case, inquires.)</p>
  
  <p>So, here’s what your plugin’s <a href="http://buildwithcraft.com/classreference/etc/components/ISavableComponentType#getSettingsHtml-detail">getSettingsHtml()</a> method should look like:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function getSettingsHtml()
  {
      $fieldLayout = craft()-&gt;fields-&gt;getLayoutByType('Inquiry');
  
      return craft()-&gt;templates-&gt;render('inquires/_settings', array(
          'settings' =&gt; $this-&gt;getSettings(),
          'fieldLayout' =&gt; $fieldLayout
      ));
  }
  </code></pre>
  
  <p>That “inquiries/_settings” template would map to craft/plugins/inquiries/templates/_settings.html, and that should include this:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% include "_includes/fieldlayoutdesigner" with {
      fieldLayout: fieldLayout
  } only %}
  </code></pre>
  
  <p>And your plugin’s <a href="http://buildwithcraft.com/classreference/etc/components/ISavableComponentType#prepSettings-detail">prepSettings()</a> method should look something like this:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function prepSettings($settings)
  {
      // Delete the old field layout
      craft()-&gt;fields-&gt;deleteLayoutsByType('Foo');
  
      // Extract the field layout data from the settings
      $postedFieldLayout = isset($settings['fieldLayout']) ? $settings['fieldLayout'] : array();
      $requiredFields = isset($settings['requiredFields']) ? $settings['requiredFields'] : array();
  
      // Save the field layout
      $fieldLayout = craft()-&gt;fields-&gt;assembleLayout($postedFieldLayout, $requiredFields, true);
      $fieldLayout-&gt;type = 'Foo';
      craft()-&gt;fields-&gt;saveLayout($fieldLayout);
  
      // Return the settings without the field layout data
      unset($settings['fieldLayout'], $settings['requiredFields']);
      return $settings;
  }
  </code></pre>
  
  <p>Finally, whatever model your element type’s <a href="http://buildwithcraft.com/classreference/elementtypes/IElementType#populateElementModel-detail">populateElementModel()</a> method returns should have a <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldLayout-detail">getFieldLayout()</a> method that looks like this:</p>
  
  <pre class="lang-twig prettyprint-override"><code>public function getFieldLayout()
  {
      return craft()-&gt;fields-&gt;getLayoutByType('Inquiry');
  }
  </code></pre>
  
  <p><em><strong>Note:</strong> The Field Layout Designer will only work within the plugin settings in <a href="http://buildwithcraft.com/updates#build2596">Craft 2.2.2596</a> or later.</em></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-21T21:36:38.663Z
Answers.LastActivityDate: 2014-10-21T21:36:38.663Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I working on a simple inquiries plugin and i want it to use custom element types to manage the inquiries.</p>
  
  <p>I have the basics working but I'm trying to setup the option to add/edit custom fields on the inquiry and thats where I'm running into some issues.  Most of the other examples I'm finding (including Pixel &amp; Tonic's own events plugin) all seem to have a second model that sets up the field layout and passes it to the elements like Calendars -> Events, or Entry Types -> Entries, Category Groups-> Categories.</p>
  
  <p>But what I'm hoping to do is just have one single field layout for all inquiries (more like how users work currently) and ideally this would be editable through the plugin settings (instead of the tab)  is that currently possible?</p>
  
  <p>I can include this:</p>
  
  <pre><code>{% include "_includes/fieldlayoutdesigner" with {
    fieldLayout: fieldLayout
  } only %}
  </code></pre>
  
  <p>but i need to get a field layout somehow? and then hook into the save?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-21T17:31:59.287Z
Comments.Text: Looks like there is a bug in Craft that is preventing this from working as it should. Going to get this fixed in the next update, and then post an answer.
Comments.UserDisplayName: Brandon Kelly
Tags: <plugins><elementtype>
Title: Is it possible to use the field layout designer in settings screen?
ThreadText: # Is it possible to use the field layout designer in settings screen?
  
  (Asked by: username_0 on 2014-10-14)
  
  I working on a simple inquiries plugin and i want it to use custom element types to manage the inquiries.
  
  I have the basics working but I'm trying to setup the option to add/edit custom fields on the inquiry and thats where I'm running into some issues. Most of the other examples I'm finding (including Pixel & Tonic's own events plugin) all seem to have a second model that sets up the field layout and passes it to the elements like Calendars -> Events, or Entry Types -> Entries, Category Groups-> Categories.
  
  But what I'm hoping to do is just have one single field layout for all inquiries (more like how users work currently) and ideally this would be editable through the plugin settings (instead of the tab) is that currently possible?
  
  I can include this:
  
  ```
  {% include "_includes/fieldlayoutdesigner" with {
    fieldLayout: fieldLayout
  } only %}
  
  ```
  
  but i need to get a field layout somehow? and then hook into the save?
  
  **Tags:** plugins, elementtype
  
  > **Comments:**
  > **username_1**: Looks like there is a bug in Craft that is preventing this from working as it should. Going to get this fixed in the next update, and then post an answer.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-10-21. Score: 6 | Accepted answer
  
  There are two ways to retrieve field layouts: by ID ([FieldsService::getLayoutById()](http://buildwithcraft.com/classreference/services/FieldsService#getLayoutById-detail)) and by element type ([FieldsService::getLayoutByType()](http://buildwithcraft.com/classreference/services/FieldsService#getLayoutByType-detail)). The former is useful for element types that manage multiple field layouts and have their own way of storing those field layout IDs (e.g. sections, asset sources, etc.), and the latter is useful for element types that only need to manage a single field layout (e.g. users, or in your case, inquires.)
  
  So, here’s what your plugin’s [getSettingsHtml()](http://buildwithcraft.com/classreference/etc/components/ISavableComponentType#getSettingsHtml-detail) method should look like:
  
  ```php
  public function getSettingsHtml()
  {
      $fieldLayout = craft()->fields->getLayoutByType('Inquiry');
  
      return craft()->templates->render('inquires/_settings', array(
          'settings' => $this->getSettings(),
          'fieldLayout' => $fieldLayout
      ));
  }
  
  ```
  
  That “inquiries/\_settings” template would map to craft/plugins/inquiries/templates/\_settings.html, and that should include this:
  
  ```twig
  {% include "_includes/fieldlayoutdesigner" with {
      fieldLayout: fieldLayout
  } only %}
  
  ```
  
  And your plugin’s [prepSettings()](http://buildwithcraft.com/classreference/etc/components/ISavableComponentType#prepSettings-detail) method should look something like this:
  
  ```php
  public function prepSettings($settings)
  {
      // Delete the old field layout
      craft()->fields->deleteLayoutsByType('Foo');
  
      // Extract the field layout data from the settings
      $postedFieldLayout = isset($settings['fieldLayout']) ? $settings['fieldLayout'] : array();
      $requiredFields = isset($settings['requiredFields']) ? $settings['requiredFields'] : array();
  
      // Save the field layout
      $fieldLayout = craft()->fields->assembleLayout($postedFieldLayout, $requiredFields, true);
      $fieldLayout->type = 'Foo';
      craft()->fields->saveLayout($fieldLayout);
  
      // Return the settings without the field layout data
      unset($settings['fieldLayout'], $settings['requiredFields']);
      return $settings;
  }
  
  ```
  
  Finally, whatever model your element type’s [populateElementModel()](http://buildwithcraft.com/classreference/elementtypes/IElementType#populateElementModel-detail) method returns should have a [getFieldLayout()](http://buildwithcraft.com/classreference/models/BaseElementModel#getFieldLayout-detail) method that looks like this:
  
  ```twig
  public function getFieldLayout()
  {
      return craft()->fields->getLayoutByType('Inquiry');
  }
  
  ```
  
  ***Note:** The Field Layout Designer will only work within the plugin settings in [Craft 2.2.2596](http://buildwithcraft.com/updates#build2596) or later.*
  
  
  ---
  
  ### Answer by username_2 on 2018-02-03. Score: 2
  
  I had the problems that I used tabs in my plugins. In one of those tabs I save settings in general and in antoher tabs I save the FieldLayout. Here is the adapted method prepSettings()
  
  ```
  public function prepSettings($settings)
  {
  
      $postedFieldLayout = craft()->request->getParam('fieldLayout') ? craft()->request->getParam('fieldLayout') : array();
      $requiredFields = craft()->request->getParam('requiredFields') ? craft()->request->getParam('requiredFields') : array();
  
      if($postedFieldLayout){
  
          // Delete the old field layout
          craft()->fields->deleteLayoutsByType('Foo');
  
          // Save the field layout
          $fieldLayout = craft()->fields->assembleLayout($postedFieldLayout, $requiredFields, true);
          $fieldLayout->type = 'Foo';
          craft()->fields->saveLayout($fieldLayout);
      }
  
      return $settings;
  }
  
  ```
  
  In the controller you prefer
  
  ```
  $fieldLayout = craft()->fields->getLayoutByType('Foo');
  // dont foget to add'em as an parameter in renderLayout
  
  ```
  
  In the model include
  
  ```
  public function getFieldLayout()
  {
      return craft()->fields->getLayoutByType('Foo');
  }
  
  ```
  
  The content block in my tabs layout
  
  ```
  <form method='post' action='' accept-charset='UTF-8' data-saveshortcut data-confirm-unload>
      {{ getCsrfInput() }}
  
      <input type='hidden' name='action' value='plugins/savePluginSettings'>
      <input type='hidden' name='pluginClass' value='FooPluginClass'>
  
      <section id='fields'>
          {% include "_includes/fieldlayoutdesigner" with {
              fieldLayout: fieldLayout
          } only %}
      </section>
  
       <div class='buttons'>
          <input class='btn submit' type='submit' value='{{ 'Save'|t }}'>
      </div>
  </form>
  
  ```
  
  
  ---
  
  