View source
<?php
namespace Drupal\ckeditor_templates\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\editor\Entity\Editor;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CkeditorTemplates extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, ContainerFactoryPluginInterface {
private $configFactoryService;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactory $configFactoryService) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactoryService = $configFactoryService;
}
public function getFile() {
return $this
->getTemplatesPluginPath() . '/plugin.js';
}
public function getLibraries(Editor $editor) {
return [
'ckeditor_templates/ckeditor.templates.dialog',
];
}
public function getButtons() {
return [
'Templates' => [
'label' => t('Templates'),
'image' => $this
->getTemplatesPluginPath() . '/icons/templates.png',
],
];
}
public function getConfig(Editor $editor) {
$config = [];
$settings = $editor
->getSettings();
if (isset($settings['plugins']['templates']['replace_content'])) {
$config['templates_replaceContent'] = $settings['plugins']['templates']['replace_content'];
}
if (isset($settings['plugins']['templates']['template_path']) && !empty($settings['plugins']['templates']['template_path'])) {
$config['templates_files'] = [
$settings['plugins']['templates']['template_path'],
];
}
else {
$config['templates_files'] = $this
->getTemplatesDefaultPath();
}
return $config;
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$config = [
'replace_content' => FALSE,
'template_path' => '',
];
$settings = $editor
->getSettings();
if (isset($settings['plugins']['templates'])) {
$config = $settings['plugins']['templates'];
}
$form['template_path'] = [
'#title' => t('Template definition file'),
'#type' => 'textfield',
'#default_value' => $config['template_path'],
'#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"'),
];
$form['replace_content'] = [
'#title' => t('Replace content default value'),
'#type' => 'checkbox',
'#default_value' => $config['replace_content'],
'#description' => t('Whether the "Replace actual contents" checkbox is checked by default in the Templates dialog'),
];
$form['#attached']['library'][] = 'ckeditor_templates/ckeditor.templates.admin';
return $form;
}
private function getTemplatesPluginPath() {
$pluginPath = 'libraries/ckeditor/plugins/templates';
if (!file_exists(DRUPAL_ROOT . '/' . $pluginPath)) {
$pluginPath = 'libraries/templates';
}
return $pluginPath;
}
private function getTemplatesDefaultPath() {
$defaultPath = base_path() . $this
->getTemplatesPluginPath() . '/templates/default.js';
$defaultThemConfig = $this->configFactoryService
->get('system.theme');
$defaultThemeName = $defaultThemConfig
->get('default');
$defaultThemeFileAbsolutePath = DRUPAL_ROOT . '/' . drupal_get_path('theme', $defaultThemeName) . '/templates/ckeditor_templates.js';
if (file_exists($defaultThemeFileAbsolutePath)) {
$defaultPath = base_path() . drupal_get_path('theme', $defaultThemeName) . '/templates/ckeditor_templates.js';
}
return [
$defaultPath,
];
}
}