在twig IvoryCKEditorBundle Symfony中加载自定义配置

时间:2018-02-08 10:09:46

标签: symfony twig ckeditor4.x

我创建了这样的表单:

$siteContent2Form = $this->get('form.factory')->createNamedBuilder('cont_form_2', CKEditorType::class, $siteContent2, array(
        'label' => false,
        'config_name' => 'ckeditor_config_std',
))->getForm();

在树枝上:

{{ form_start(form_content_2) }}
{{ form_widget(form_content_2) }}
{{ form_end(form_content_2) }}

是否可以覆盖加载的配置“ckeditor_config_std”并在模板(twig)中加载另一个?

1 个答案:

答案 0 :(得分:0)

如果不创建Twig扩展,我认为你不能这样做。这是我发现的:

我跟着Symfony's tutorial建立了一个包含三个字段的简单表单。 task字段是CKEditor字段:

$form = $this->createFormBuilder($task)
    ->add('task', CKEditorType::class, [
        'config_name' => 'my_config',
        'config' => [
            'uiColor' => '#c0ffee',
        ],
    ])
    ->add('dueDate', DateType::class)
    ->add('save', SubmitType::class, ['label' => 'Create task'])
    ->getForm();

让我们看看我们在Twig中有什么:

{{ dump(form) }}
FormView {#398 ▼
    +vars: array:24 [▶]
    +parent: null
    +children: array:3 [▼
        "task" => FormView {#403 ▶}
        "dueDate" => FormView {#405 ▶}
        "save" => FormView {#327 ▶}
    ]
    -rendered: false
    -methodRendered: false
}

查看配置选项的位置(uiColor配置来自PHP,如上所示,而toolbar配置来自YAML配置):

FormView {#398 ▼
    +children: array:3 [▼
        "task" => FormView {#403 ▼
            +vars: array:41 [▼
                "config" => array:2 [▼
                    "toolbar" => array:1 [▶]
                    "uiColor" => "#c0ffee"
                ]
            ]
        }
    ]
}

FormView类似乎没有提供任何更改变量的方法:https://api.symfony.com/3.4/Symfony/Component/Form/FormView.html

您既不能使用merge过滤器,因为FormView对象将转换为数组,然后您无法呈现表单:

{% set form = form|merge({}) %}

{{ dump(form) }}
array:3 [▼
    "task" => FormView {#403 ▶}
    "dueDate" => FormView {#405 ▶}
    "save" => FormView {#327 ▶}
]

你可以创建一个简单的Twig扩展,如下所示:

<?php

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class AppExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            new TwigFunction('set_ckeditor_config', [$this, 'SetCKEditorConfig']),
        ];
    }

    public function SetCKEditorConfig($formView, $name, $value)
    {
        $formView->vars['config'][$name] = $value;
    }
}

然后在Twig:

{{ dump(form.task.vars.config) }}

{% do set_ckeditor_config(form.task, 'uiColor', '#bada55') %}
{% do set_ckeditor_config(form.task, 'height', 500) %}

{{ dump(form.task.vars.config) }}

{# You should render the form *after* calling the set_ckeditor_config function #}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
array:2 [▼
    "toolbar" => array:1 [▶]
    "uiColor" => "#c0ffee"
]

array:3 [▼
    "toolbar" => array:1 [▶]
    "uiColor" => "#bada55"
    "height" => 500
]

如您所见,这样您就可以修改单个配置选项。这不完全是你问的,但它很接近,对吧?

您可以修改SetCKEditorConfig,以便一次设置多个配置。然后你只需要在Twig中调用set_ckeditor_config一次。如果您想要更改许多配置,那可能会更好。

您还可以尝试修改代码,以便在您的案例中选择不同的config_name(即ckeditor_config_std以外的其他内容)。这似乎是一件更复杂的事情,所以我放弃了。

我希望这会引导你走上正轨。