1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSCKEditor Bundle. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 - present Friends of Symfony |
7
|
|
|
* (c) 2009 - 2017 Eric GELOEN <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please read the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace FOS\CKEditorBundle\Form\Type; |
14
|
|
|
|
15
|
|
|
use FOS\CKEditorBundle\Config\CKEditorConfigurationInterface; |
16
|
|
|
use Symfony\Component\Form\AbstractType; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType; |
18
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
19
|
|
|
use Symfony\Component\Form\FormInterface; |
20
|
|
|
use Symfony\Component\Form\FormView; |
21
|
|
|
use Symfony\Component\OptionsResolver\Options; |
22
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class CKEditorType extends AbstractType |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var CKEditorConfigurationInterface |
31
|
|
|
*/ |
32
|
|
|
private $configuration; |
33
|
|
|
|
34
|
|
|
public function __construct(CKEditorConfigurationInterface $configuration) |
35
|
|
|
{ |
36
|
|
|
$this->configuration = $configuration; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void |
40
|
|
|
{ |
41
|
|
|
$builder->setAttribute('enable', $options['enable']); |
42
|
|
|
|
43
|
|
|
if (!$options['enable']) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$builder->setAttribute('async', $options['async']); |
48
|
|
|
$builder->setAttribute('autoload', $options['autoload']); |
49
|
|
|
$builder->setAttribute('auto_inline', $options['auto_inline']); |
50
|
|
|
$builder->setAttribute('inline', $options['inline']); |
51
|
|
|
$builder->setAttribute('jquery', $options['jquery']); |
52
|
|
|
$builder->setAttribute('require_js', $options['require_js']); |
53
|
|
|
$builder->setAttribute('input_sync', $options['input_sync']); |
54
|
|
|
$builder->setAttribute('filebrowsers', $options['filebrowsers']); |
55
|
|
|
$builder->setAttribute('base_path', $options['base_path']); |
56
|
|
|
$builder->setAttribute('js_path', $options['js_path']); |
57
|
|
|
$builder->setAttribute('jquery_path', $options['jquery_path']); |
58
|
|
|
$builder->setAttribute('config', $this->resolveConfig($options)); |
59
|
|
|
$builder->setAttribute('config_name', $options['config_name']); |
60
|
|
|
$builder->setAttribute('plugins', array_merge($this->configuration->getPlugins(), $options['plugins'])); |
61
|
|
|
$builder->setAttribute('styles', array_merge($this->configuration->getStyles(), $options['styles'])); |
62
|
|
|
$builder->setAttribute('templates', array_merge($this->configuration->getTemplates(), $options['templates'])); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function resolveConfig(array $options): array |
66
|
|
|
{ |
67
|
|
|
$config = $options['config']; |
68
|
|
|
|
69
|
|
|
if (null === $options['config_name']) { |
70
|
|
|
$options['config_name'] = uniqid('fos', true); |
71
|
|
|
} else { |
72
|
|
|
$config = array_merge($this->configuration->getConfig($options['config_name']), $config); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($config['toolbar']) && is_string($config['toolbar'])) { |
76
|
|
|
$config['toolbar'] = $this->configuration->getToolbar($config['toolbar']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $config; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options): void |
83
|
|
|
{ |
84
|
|
|
$config = $form->getConfig(); |
85
|
|
|
$view->vars['enable'] = $config->getAttribute('enable'); |
86
|
|
|
|
87
|
|
|
if (!$view->vars['enable']) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$view->vars['async'] = $config->getAttribute('async'); |
92
|
|
|
$view->vars['autoload'] = $config->getAttribute('autoload'); |
93
|
|
|
$view->vars['auto_inline'] = $config->getAttribute('auto_inline'); |
94
|
|
|
$view->vars['inline'] = $config->getAttribute('inline'); |
95
|
|
|
$view->vars['jquery'] = $config->getAttribute('jquery'); |
96
|
|
|
$view->vars['require_js'] = $config->getAttribute('require_js'); |
97
|
|
|
$view->vars['input_sync'] = $config->getAttribute('input_sync'); |
98
|
|
|
$view->vars['filebrowsers'] = $config->getAttribute('filebrowsers'); |
99
|
|
|
$view->vars['base_path'] = $config->getAttribute('base_path'); |
100
|
|
|
$view->vars['js_path'] = $config->getAttribute('js_path'); |
101
|
|
|
$view->vars['jquery_path'] = $config->getAttribute('jquery_path'); |
102
|
|
|
$view->vars['config'] = $config->getAttribute('config'); |
103
|
|
|
$view->vars['config_name'] = $config->getAttribute('config_name'); |
104
|
|
|
$view->vars['plugins'] = $config->getAttribute('plugins'); |
105
|
|
|
$view->vars['styles'] = $config->getAttribute('styles'); |
106
|
|
|
$view->vars['templates'] = $config->getAttribute('templates'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
110
|
|
|
{ |
111
|
|
|
$resolver |
112
|
|
|
->setDefaults([ |
113
|
|
|
'enable' => $this->configuration->isEnable(), |
114
|
|
|
'async' => $this->configuration->isAsync(), |
115
|
|
|
'autoload' => $this->configuration->isAutoload(), |
116
|
|
|
'auto_inline' => $this->configuration->isAutoInline(), |
117
|
|
|
'inline' => $this->configuration->isInline(), |
118
|
|
|
'jquery' => $this->configuration->isJquery(), |
119
|
|
|
'require_js' => $this->configuration->isRequireJs(), |
120
|
|
|
'input_sync' => $this->configuration->isInputSync(), |
121
|
|
|
'filebrowsers' => $this->configuration->getFilebrowsers(), |
122
|
|
|
'base_path' => $this->configuration->getBasePath(), |
123
|
|
|
'js_path' => $this->configuration->getJsPath(), |
124
|
|
|
'jquery_path' => $this->configuration->getJqueryPath(), |
125
|
|
|
'config_name' => $this->configuration->getDefaultConfig(), |
126
|
|
|
'config' => [], |
127
|
|
|
'plugins' => [], |
128
|
|
|
'styles' => [], |
129
|
|
|
'templates' => [], |
130
|
|
|
]) |
131
|
|
|
->addAllowedTypes('enable', 'bool') |
132
|
|
|
->addAllowedTypes('async', 'bool') |
133
|
|
|
->addAllowedTypes('autoload', 'bool') |
134
|
|
|
->addAllowedTypes('auto_inline', 'bool') |
135
|
|
|
->addAllowedTypes('inline', 'bool') |
136
|
|
|
->addAllowedTypes('jquery', 'bool') |
137
|
|
|
->addAllowedTypes('require_js', 'bool') |
138
|
|
|
->addAllowedTypes('input_sync', 'bool') |
139
|
|
|
->addAllowedTypes('filebrowsers', 'array') |
140
|
|
|
->addAllowedTypes('config_name', ['string', 'null']) |
141
|
|
|
->addAllowedTypes('base_path', 'string') |
142
|
|
|
->addAllowedTypes('js_path', 'string') |
143
|
|
|
->addAllowedTypes('jquery_path', 'string') |
144
|
|
|
->addAllowedTypes('config', 'array') |
145
|
|
|
->addAllowedTypes('plugins', 'array') |
146
|
|
|
->addAllowedTypes('styles', 'array') |
147
|
|
|
->addAllowedTypes('templates', 'array') |
148
|
|
|
->setNormalizer('base_path', function (Options $options, $value) { |
149
|
|
|
if ('/' !== substr($value, -1)) { |
150
|
|
|
$value .= '/'; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $value; |
154
|
|
|
}); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getParent(): string |
158
|
|
|
{ |
159
|
|
|
return TextareaType::class; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getBlockPrefix(): string |
163
|
|
|
{ |
164
|
|
|
return 'ckeditor'; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|