1

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);     
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);
}
3
  • 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. May 8 '18 at 17:01
  • 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 May 8 '18 at 17:02
  • Thanks for checking this out; I tried it again with fresh install and simple migration, and everything worked great. Appreciate the help! May 8 '18 at 20:50
1

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'
                        ]
                    ]
                ]
            ]
       ]
]);

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.