2

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?

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' => '']
    ]
]

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.