2

In a plugin's settings view, I'm looking to show a list of fields to choose from:

{% set fields = craft.fields.getAllFields() %}  

{{ forms.selectField({
    label:          'Salary Mapping'|t,
    id:             'broadbeanSalaryMapping',
    name:           'broadbeanSalaryMapping',
    instructions:   'Choose a field in which to store this value.'|t,
    value:          settings.broadbeanSalaryMapping,
    options:        fields,
    errors:         settings.getErrors('broadbeanSalaryMapping')
}) }}

However I get an error:

Craft\FieldModel and its behaviors do not have a method or closure named "optgroup".

Am I going about this the wrong way?

4

I've got a solution, though I'd be interested to know if there is a better way of doing it. In the plugin file:

public function getSettingsHtml()
{
    $options = [[
        'label' => 'Please select', 
        'value' => ''
    ]];

    foreach (craft()->fields->getAllFields() as $field)
    {
        $options[] = [
            'label' => $field['name'], 
            'value' => $field['handle']
        ];
    }

    return craft()->templates->render('broadbean/settings', array(
        'settings' => $this->getSettings(), 
        'options' => $options
    ));
}

And then in the settings template:

{{ forms.selectField({
    label:          'Salary Mapping'|t,
    id:             'broadbeanSalaryMapping',
    name:           'broadbeanSalaryMapping',
    instructions:   'Choose a field in which to store this value.'|t,
    value:          settings.broadbeanSalaryMapping,
    options:        options,
    errors:         settings.getErrors('broadbeanSalaryMapping')
}) }}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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