<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>

CC BY-SA 3.0
2018-02-03T00:32:30.733Z
2018-02-03T00:32:30.733Z
Simon Franzen
<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>

CC BY-SA 3.0
2014-10-21T21:36:38.663Z
2014-10-21T21:36:38.663Z
Brandon Kelly
<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>

CC BY-SA 3.0
2014-10-21T17:31:59.287Z
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.
Brandon Kelly
<plugins><elementtype>
Is it possible to use the field layout designer in settings screen?
# 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()) and by element type (FieldsService::getLayoutByType()). 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() method should look like:
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:
{% include "_includes/fieldlayoutdesigner" with {
    fieldLayout: fieldLayout
} only %}

And your plugin’s prepSettings() method should look something like this:
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() method returns should have a getFieldLayout() method that looks like this:
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 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

    {{ getCsrfInput() }}

    
    

    
        {% include "_includes/fieldlayoutdesigner" with {
            fieldLayout: fieldLayout
        } only %}


---

