Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / ckeditor_templates / src / Plugin / CKEditorPlugin / CkeditorTemplates.php
1 <?php
2
3 namespace Drupal\ckeditor_templates\Plugin\CKEditorPlugin;
4
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;
12
13 /**
14  * Defines the "Templates" plugin.
15  *
16  * @CKEditorPlugin(
17  *   id = "templates",
18  *   label = @Translation("Templates")
19  * )
20  */
21 class CkeditorTemplates extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, ContainerFactoryPluginInterface {
22
23   /**
24    * Configuration Factory Service.
25    *
26    * @var \Drupal\Core\Config\ConfigFactory
27    */
28   private $configFactoryService;
29
30   /**
31    * {@inheritdoc}
32    */
33   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
34     return new static(
35       $configuration, $plugin_id, $plugin_definition, $container->get('config.factory')
36     );
37   }
38
39   /**
40    * Constructs a Drupal\Component\Plugin\PluginBase object.
41    *
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.
50    */
51   public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactory $configFactoryService) {
52     parent::__construct($configuration, $plugin_id, $plugin_definition);
53
54     $this->configFactoryService = $configFactoryService;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public function getFile() {
61     return $this->getTemplatesPluginPath() . '/plugin.js';
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getLibraries(Editor $editor) {
68     return ['ckeditor_templates/ckeditor.templates.dialog'];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getButtons() {
75     return [
76       'Templates' => [
77         'label' => t('Templates'),
78         'image' => $this->getTemplatesPluginPath() . '/icons/templates.png',
79       ],
80     ];
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getConfig(Editor $editor) {
87     $config = [];
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'];
92     }
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']];
96     }
97     else {
98       // Use templates plugin default file.
99       $config['templates_files'] = $this->getTemplatesDefaultPath();
100     }
101     return $config;
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
108     // Defaults.
109     $config = [
110       'replace_content' => FALSE,
111       'template_path' => '',
112     ];
113
114     $settings = $editor->getSettings();
115
116     if (isset($settings['plugins']['templates'])) {
117       $config = $settings['plugins']['templates'];
118     }
119
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"'),
125     ];
126
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'),
132     ];
133
134     $form['#attached']['library'][] = 'ckeditor_templates/ckeditor.templates.admin';
135
136     return $form;
137   }
138
139   /**
140    * Return ckeditor templates plugin path relative to drupal root.
141    *
142    * @return string
143    *   Relative path to the ckeditor plugin folder
144    */
145   private function getTemplatesPluginPath() {
146     return 'libraries/templates';
147   }
148
149   /**
150    * Generate the path to the template file.
151    *
152    * The file will be picked from :
153    * - the default theme if the file exists
154    * - the ckeditor template directory otherwise.
155    *
156    * @return array
157    *   List of path to the template file
158    */
159   private function getTemplatesDefaultPath() {
160     // Default to module folder.
161     $defaultPath = '/' . $this->getTemplatesPluginPath() . '/templates/default.js';
162
163     // Get site default theme name.
164     $defaultThemConfig = $this->configFactoryService->get('system.theme');
165     $defaultThemeName = $defaultThemConfig->get('default');
166
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';
170     }
171
172     return [$defaultPath];
173   }
174
175 }