<p>I wanted to follow up on this for anyone else who might encounter the same issue.</p>

<p>My problem was trying to set the redactor config within a programatically generated Matrix field.</p>

<p>In this case you need to use the <code>typesettings</code> keyword, for example:</p>

<pre><code>$fields-&gt;createField([
        'type' =&gt; 'craft\\fields\\Matrix',
        'groupId' =&gt; 1,
        'name' =&gt; 'Content Builder',
        'handle' =&gt; 'contentBuilder',
        'blockTypes' =&gt; [
            'new1' =&gt; [
                'name' =&gt; 'Text Block',
                'handle' =&gt; 'textBlock',
                'instructions' =&gt; 'A rich text content block',
                'fields' =&gt; [
                    'new1' =&gt; [
                        'type' =&gt; 'craft\\redactor\\Field',
                        'name' =&gt; 'Rich Text',
                        'handle' =&gt; 'richText',                            
                        'typesettings' =&gt; [
                            'redactorConfig' =&gt; 'Simple.json'
                        ]
                    ]
                ]
            ]
       ]
]);
</code></pre>

CC BY-SA 4.0
2018-05-09T09:17:22.323Z
2018-05-09T09:17:22.323Z
Piaras Hoban
<p>Maybe you should take a look what the function <a href="https://docs.craftcms.com/api/v3/craft-services-fields.html#createField()-detail" rel="nofollow noreferrer">createField</a> does</p>
<blockquote>
<p>Creates a field with a given config.</p>
<p>return    <a href="https://docs.craftcms.com/api/v3/craft-base-fieldinterface.html" rel="nofollow noreferrer">craft\base\FieldInterface</a>  The field</p>
</blockquote>
<p>It only creates a PHP object but does not save the field at all.</p>
<pre><code>Craft::$app-&gt;getFields()-&gt;saveField($field);
</code></pre>
<p>is what you want</p>
<p>This is what the <code>FieldsController</code> does to store a new field</p>
<pre><code>public function actionSaveField()
{
    $this-&gt;requirePostRequest();

    $fieldsService = Craft::$app-&gt;getFields();
    $request = Craft::$app-&gt;getRequest();
    $type = $request-&gt;getRequiredBodyParam('type');

    $field = $fieldsService-&gt;createField([
        'type' =&gt; $type,
        'id' =&gt; $request-&gt;getBodyParam('fieldId'),
        'groupId' =&gt; $request-&gt;getRequiredBodyParam('group'),
        'name' =&gt; $request-&gt;getBodyParam('name'),
        'handle' =&gt; $request-&gt;getBodyParam('handle'),
        'instructions' =&gt; $request-&gt;getBodyParam('instructions'),
        'translationMethod' =&gt; $request-&gt;getBodyParam('translationMethod', Field::TRANSLATION_METHOD_NONE),
        'translationKeyFormat' =&gt; $request-&gt;getBodyParam('translationKeyFormat'),
        'settings' =&gt; $request-&gt;getBodyParam('types.'.$type),
    ]);

    if (!$fieldsService-&gt;saveField($field)) {
        Craft::$app-&gt;getSession()-&gt;setError(Craft::t('app', 'Couldn’t save field.'));

        // Send the field back to the template
        Craft::$app-&gt;getUrlManager()-&gt;setRouteParams([
            'field' =&gt; $field
        ]);

        return null;
    }

    Craft::$app-&gt;getSession()-&gt;setNotice(Craft::t('app', 'Field saved.'));

    return $this-&gt;redirectToPostedUrl($field);
}
</code></pre>

CC BY-SA 4.0
2018-05-08T17:01:39.167Z
Thanks for your response; I understand how to create and save fields. This question is related to specifying the config file when creating a Redactor field.
Piaras Hoban
CC BY-SA 4.0
2018-05-08T17:02:57.697Z
When I copy your code into my project everything works fine. I can define whatever redactor config I want. So I thought you are missing the line. Does your field validation fail? Most questions of these type can be solved by looking at the errors
Robin Schambach
CC BY-SA 4.0
2018-05-08T20:50:46.220Z
Thanks for checking this out; I tried it again with fresh install and simple migration, and everything worked great. Appreciate the help!
Piaras Hoban
CC BY-SA 4.0
2018-05-08T16:55:58.593Z
2018-05-08T16:55:58.593Z
2020-06-17T08:56:27.903Z
Robin Schambach
<p>is it possible to specify the Redactor config file when creating a Redactor field programmatically? </p>

<p>I was hoping I could do something like the following but have had no success:</p>

<pre><code>$bodyField = $fields&gt;createField([
    'type' =&gt; 'craft\\redactor\\Field',
    'groupId' =&gt; 1,
    'name' =&gt; 'Body',
    'handle' =&gt; 'body',
    'settings' =&gt; [
       'redactorConfig' =&gt; 'Simple.json'
    ]
]);

$fields-&gt;saveField($bodyField);     
</code></pre>

<craft3><redactor><php>
Specify Redactor config in migration
# Specify Redactor config in migration

(Asked by: username_0 on 2018-05-08)

is it possible to specify the Redactor config file when creating a Redactor field programmatically? 
I was hoping I could do something like the following but have had no success:
$bodyField = $fields>createField([
    'type' => 'craft\\redactor\\Field',
    'groupId' => 1,
    'name' => 'Body',
    'handle' => 'body',
    'settings' => [
       'redactorConfig' => 'Simple.json'
    ]
]);

$fields->saveField($bodyField);

**Tags:** craft3, redactor, php

---

## 2 answers

### Answer by username_0 on 2018-05-09. Score: 1 | Accepted answer

I wanted to follow up on this for anyone else who might encounter the same issue.
My problem was trying to set the redactor config within a programatically generated Matrix field.
In this case you need to use the typesettings keyword, for example:
$fields->createField([
        'type' => 'craft\\fields\\Matrix',
        'groupId' => 1,
        'name' => 'Content Builder',
        'handle' => 'contentBuilder',
        'blockTypes' => [
            'new1' => [
                'name' => 'Text Block',
                'handle' => 'textBlock',
                'instructions' => 'A rich text content block',
                'fields' => [
                    'new1' => [
                        'type' => 'craft\\redactor\\Field',
                        'name' => 'Rich Text',
                        'handle' => 'richText',                            
                        'typesettings' => [
                            'redactorConfig' => 'Simple.json'
                        ]
                    ]
                ]
            ]
       ]
]);


---

### Answer by username_1 on 2018-05-08. Score: 1

Maybe you should take a look what the function createField does

Creates a field with a given config.
return    craft\base\FieldInterface  The field

It only creates a PHP object but does not save the field at all.
Craft::$app->getFields()->saveField($field);

is what you want
This is what the FieldsController does to store a new field
public function actionSaveField()
{
    $this->requirePostRequest();

    $fieldsService = Craft::$app->getFields();
    $request = Craft::$app->getRequest();
    $type = $request->getRequiredBodyParam('type');

    $field = $fieldsService->createField([
        'type' => $type,
        'id' => $request->getBodyParam('fieldId'),
        'groupId' => $request->getRequiredBodyParam('group'),
        'name' => $request->getBodyParam('name'),
        'handle' => $request->getBodyParam('handle'),
        'instructions' => $request->getBodyParam('instructions'),
        'translationMethod' => $request->getBodyParam('translationMethod', Field::TRANSLATION_METHOD_NONE),
        'translationKeyFormat' => $request->getBodyParam('translationKeyFormat'),
        'settings' => $request->getBodyParam('types.'.$type),
    ]);

    if (!$fieldsService->saveField($field)) {
        Craft::$app->getSession()->setError(Craft::t('app', 'Couldn’t save field.'));

        // Send the field back to the template
        Craft::$app->getUrlManager()->setRouteParams([
            'field' => $field
        ]);

        return null;
    }

    Craft::$app->getSession()->setNotice(Craft::t('app', 'Field saved.'));

    return $this->redirectToPostedUrl($field);
}

> **Comments:**
> **username_0**: Thanks for your response; I understand how to create and save fields. This question is related to specifying the config file when creating a Redactor field.  
> **username_1**: When I copy your code into my project everything works fine. I can define whatever redactor config I want. So I thought you are missing the line. Does your field validation fail? Most questions of these type can be solved by looking at the errors  
> **username_0**: Thanks for checking this out; I tried it again with fresh install and simple migration, and everything worked great. Appreciate the help!  

---

