<p>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:</p>

<pre><code>public function getSettingsHtml()
{
    $options = [[
        'label' =&gt; 'Please select', 
        'value' =&gt; ''
    ]];

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

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

<p>And then in the settings template:</p>

<pre><code>{{ 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')
}) }}
</code></pre>

CC BY-SA 3.0
2014-10-31T22:04:11.500Z
2014-10-31T22:04:11.500Z
Russ Back
<p>In a plugin's settings view, I'm looking to show a list of fields to choose from:</p>

<pre><code>{% 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')
}) }}
</code></pre>

<p>However I get an error:</p>

<blockquote>
  <p>Craft\FieldModel and its behaviors do not have a method or closure named "optgroup". </p>
</blockquote>

<p>Am I going about this the wrong way?</p>

<plugin-development><field>
Displaying a list of fields in a form select
# Displaying a list of fields in a form select

(Asked by: username_0 on 2014-10-31)

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?

**Tags:** plugin-development, field

---

## 1 answers

### Answer by username_0 on 2014-10-31. Score: 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')
}) }}


---

