<p>I figured it out! Field-specific options need to go inside 'typesettings'.</p>
<pre><code>'typesettings' =&gt; [
    'options' =&gt; [
        ['label' =&gt; 'H1', 'value' =&gt; 'h1', 'default' =&gt; 1],
        ['label' =&gt; 'H2', 'value' =&gt; 'h2', 'default' =&gt; ''],
        ['label' =&gt; 'H3', 'value' =&gt; 'h3', 'default' =&gt; '']
    ]
]
</code></pre>

CC BY-SA 4.0
2020-10-06T19:08:38.400Z
2020-10-06T19:08:38.400Z
dmatthams
<p>I'm building a Matrix field from within a module's migration.</p>
<p>I can get all fields setup correctly except the options of either a Dropdown or Checkboxes field.</p>
<pre><code>$field = new \craft\fields\Matrix([
    'groupId' =&gt; 1,
    'name' =&gt; $fieldSettings['name'],
    'handle' =&gt; $fieldSettings['handle']
])

$blocks = [
    'new1' =&gt; [
        'name' =&gt; 'Heading',
        'handle' =&gt; 'heading',
        'fields' =&gt; [
            'new1' =&gt; [
                'type' =&gt; \craft\fields\Dropdown::class,
                'name' =&gt; 'Heading',
                'handle' =&gt; 'pageContentHeading',
                'options' =&gt; [
                     ['label' =&gt; 'H1', 'value' =&gt; 'h1', 'default' =&gt; 1],
                     ['label' =&gt; 'H2', 'value' =&gt; 'h2', 'default' =&gt; ''],
                     ['label' =&gt; 'H3', 'value' =&gt; 'h3', 'default' =&gt; '']
                 ]
             ]
         ]
     ]
]

$field-&gt;setBlockTypes($blocks);

try {
    Craft::$app-&gt;getFields()-&gt;saveField($field);
} catch (\Throwable $e) {
    echo $e-&gt;getMessage();
}
</code></pre>
<p>This creates the dropdown field, but doesn't set the options. Am I missing something obvious?</p>

<plugin-development><migration><modules>
Programatically setting dropdown options in a matrix
# Programatically setting dropdown options in a matrix

(Asked by: username_0 on 2020-10-06)

I'm building a Matrix field from within a module's migration.
I can get all fields setup correctly except the options of either a Dropdown or Checkboxes field.
$field = new \craft\fields\Matrix([
    'groupId' => 1,
    'name' => $fieldSettings['name'],
    'handle' => $fieldSettings['handle']
])

$blocks = [
    'new1' => [
        'name' => 'Heading',
        'handle' => 'heading',
        'fields' => [
            'new1' => [
                'type' => \craft\fields\Dropdown::class,
                'name' => 'Heading',
                'handle' => 'pageContentHeading',
                'options' => [
                     ['label' => 'H1', 'value' => 'h1', 'default' => 1],
                     ['label' => 'H2', 'value' => 'h2', 'default' => ''],
                     ['label' => 'H3', 'value' => 'h3', 'default' => '']
                 ]
             ]
         ]
     ]
]

$field->setBlockTypes($blocks);

try {
    Craft::$app->getFields()->saveField($field);
} catch (\Throwable $e) {
    echo $e->getMessage();
}

This creates the dropdown field, but doesn't set the options. Am I missing something obvious?

**Tags:** plugin-development, migration, modules

---

## 1 answers

### Answer by username_0 on 2020-10-06. Score: 2

I figured it out! Field-specific options need to go inside 'typesettings'.
'typesettings' => [
    'options' => [
        ['label' => 'H1', 'value' => 'h1', 'default' => 1],
        ['label' => 'H2', 'value' => 'h2', 'default' => ''],
        ['label' => 'H3', 'value' => 'h3', 'default' => '']
    ]
]


---

