2

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?

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. Commented Oct 21, 2014 at 17:31

2 Answers 2

6

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.

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.