3 namespace Drupal\ckeditor_templates\Plugin\CKEditorPlugin;
5 use Drupal\ckeditor\CKEditorPluginBase;
6 use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
7 use Drupal\Core\Config\ConfigFactory;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\editor\Entity\Editor;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Defines the "Templates" plugin.
18 * label = @Translation("Templates")
21 class CkeditorTemplates extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, ContainerFactoryPluginInterface {
24 * Configuration Factory Service.
26 * @var \Drupal\Core\Config\ConfigFactory
28 private $configFactoryService;
33 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
35 $configuration, $plugin_id, $plugin_definition, $container->get('config.factory')
40 * Constructs a Drupal\Component\Plugin\PluginBase object.
42 * @param array $configuration
43 * A configuration array containing information about the plugin instance.
44 * @param string $plugin_id
45 * The plugin_id for the plugin instance.
46 * @param mixed $plugin_definition
47 * The plugin implementation definition.
48 * @param \Drupal\Core\Config\ConfigFactory $configFactoryService
49 * Drupal Configuration Factory Service.
51 public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactory $configFactoryService) {
52 parent::__construct($configuration, $plugin_id, $plugin_definition);
54 $this->configFactoryService = $configFactoryService;
60 public function getFile() {
61 return $this->getTemplatesPluginPath() . '/plugin.js';
67 public function getLibraries(Editor $editor) {
68 return ['ckeditor_templates/ckeditor.templates.dialog'];
74 public function getButtons() {
77 'label' => t('Templates'),
78 'image' => $this->getTemplatesPluginPath() . '/icons/templates.png',
86 public function getConfig(Editor $editor) {
88 $settings = $editor->getSettings();
89 // Set replace content default value if set.
90 if (isset($settings['plugins']['templates']['replace_content'])) {
91 $config['templates_replaceContent'] = $settings['plugins']['templates']['replace_content'];
93 // Set template files default value if set.
94 if (isset($settings['plugins']['templates']['template_path']) && !empty($settings['plugins']['templates']['template_path'])) {
95 $config['templates_files'] = [$settings['plugins']['templates']['template_path']];
98 // Use templates plugin default file.
99 $config['templates_files'] = $this->getTemplatesDefaultPath();
107 public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
110 'replace_content' => FALSE,
111 'template_path' => '',
114 $settings = $editor->getSettings();
116 if (isset($settings['plugins']['templates'])) {
117 $config = $settings['plugins']['templates'];
120 $form['template_path'] = [
121 '#title' => t('Template definition file'),
122 '#type' => 'textfield',
123 '#default_value' => $config['template_path'],
124 '#description' => t('Path to the javascript file defining the templates, relative to drupal root (starting with "/"). By default, it looks in your default theme directory for a file named "templates/ckeditor_templates.js"'),
127 $form['replace_content'] = [
128 '#title' => t('Replace content default value'),
129 '#type' => 'checkbox',
130 '#default_value' => $config['replace_content'],
131 '#description' => t('Whether the "Replace actual contents" checkbox is checked by default in the Templates dialog'),
134 $form['#attached']['library'][] = 'ckeditor_templates/ckeditor.templates.admin';
140 * Return ckeditor templates plugin path relative to drupal root.
143 * Relative path to the ckeditor plugin folder
145 private function getTemplatesPluginPath() {
146 return 'libraries/templates';
150 * Generate the path to the template file.
152 * The file will be picked from :
153 * - the default theme if the file exists
154 * - the ckeditor template directory otherwise.
157 * List of path to the template file
159 private function getTemplatesDefaultPath() {
160 // Default to module folder.
161 $defaultPath = '/' . $this->getTemplatesPluginPath() . '/templates/default.js';
163 // Get site default theme name.
164 $defaultThemConfig = $this->configFactoryService->get('system.theme');
165 $defaultThemeName = $defaultThemConfig->get('default');
167 $defaultThemeFileAbsolutePath = DRUPAL_ROOT . '/' . drupal_get_path('theme', $defaultThemeName) . '/templates/ckeditor_templates.js';
168 if (file_exists($defaultThemeFileAbsolutePath)) {
169 $defaultPath = '/' . drupal_get_path('theme', $defaultThemeName) . '/templates/ckeditor_templates.js';
172 return [$defaultPath];