vendor/friendsofsymfony/ckeditor-bundle/src/Form/Type/CKEditorType.php line 27

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