Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save junaidpv/1872a1f0b5cbb3ce74105f6b3ecf04ed to your computer and use it in GitHub Desktop.
Save junaidpv/1872a1f0b5cbb3ce74105f6b3ecf04ed to your computer and use it in GitHub Desktop.
diff --git a/config/install/wysiwyg_template.settings.yml b/config/install/wysiwyg_template.settings.yml
index 8b3a650..b2380fd 100644
--- a/config/install/wysiwyg_template.settings.yml
+++ b/config/install/wysiwyg_template.settings.yml
@@ -1 +1,3 @@
library_path: 'libraries/templates'
+editor_css: ''
+view_css: ''
diff --git a/config/schema/wysiwyg_template.schema.yml b/config/schema/wysiwyg_template.schema.yml
index 46c694a..857962c 100644
--- a/config/schema/wysiwyg_template.schema.yml
+++ b/config/schema/wysiwyg_template.schema.yml
@@ -5,6 +5,12 @@ wysiwyg_template.settings:
library_path:
type: path
label: 'Root-relative path of the CKEditor Templates plugin directory'
+ editor_css:
+ type: text
+ label: 'CSS to be used in editor'
+ view_css:
+ type: text
+ label: 'CSS to be used in content view'
wysiwyg_template.wysiwyg_template.*:
type: config_entity
diff --git a/src/Form/SettingsForm.php b/src/Form/SettingsForm.php
new file mode 100644
index 0000000..816ba3b
--- /dev/null
+++ b/src/Form/SettingsForm.php
@@ -0,0 +1,122 @@
+<?php
+
+namespace Drupal\wysiwyg_template\Form;
+
+use Drupal\Core\Form\ConfigFormBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\File\FileSystemInterface;
+use Drupal\Core\Messenger\MessengerInterface;
+use Symfony\Component\Finder\Finder;
+
+/**
+ * WYSIWYG Template settings.
+ */
+class SettingsForm extends ConfigFormBase {
+ /** @var string Config settings */
+ const SETTINGS = 'wysiwyg_template.settings';
+
+ const CSS_FILE_DIRECTORY = 'public://mce';
+
+ const EDITOR_CSS_FILE_NAME = 'editor.css';
+
+ const VIEW_CSS_FILE_NAME = 'view.css';
+
+ public function getFormId() {
+ return 'wysiwyg_template_settings';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getEditableConfigNames() {
+ return [
+ static::SETTINGS,
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(array $form, FormStateInterface $form_state) {
+ $config = $this->config(static::SETTINGS);
+
+ $form['editor_css'] = [
+ '#type' => 'textarea',
+ '#title' => $this->t('Editor CSS'),
+ '#default_value' => $config->get('editor_css'),
+ '#description' => $this->t('CSS to be used for WYSIWYG templates in editor.'),
+ ];
+
+ $form['view_css'] = [
+ '#type' => 'textarea',
+ '#title' => $this->t('View CSS'),
+ '#default_value' => $config->get('view_css'),
+ '#description' => $this->t('CSS to be used when WYSIWYG template are being used on content view pages.'),
+ ];
+
+ return parent::buildForm($form, $form_state);
+ }
+
+ public function validateForm(array &$form, FormStateInterface $form_state) {
+ $directory = static::CSS_FILE_DIRECTORY;
+ if (! \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
+ $form_state->setErrorByName('editor_css', t('CSS content cannot be written to file.'));
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitForm(array &$form, FormStateInterface $form_state) {
+ // Retrieve the configuration
+ $this->configFactory->getEditable(static::SETTINGS)
+ // Set the submitted editor CSS setting
+ ->set('editor_css', $form_state->getValue('editor_css'))
+ ->set('view_css', $form_state->getValue('view_css'))
+ ->save();
+
+ if ($new_editor_css_file_name = static::regenerateCSSFile('editor', $form_state->getValue('editor_css'))) {
+ \Drupal::state()->set('wysiwyg_template.editor_css_file', $new_editor_css_file_name);
+ $message = $this->t('Editor CSS has been written to file "@file."', ['@file'=> $new_editor_css_file_name]);
+ $status = MessengerInterface::TYPE_STATUS;
+ }
+ else {
+ $message = $this->t('Editor CSS could not be written to file.');
+ $status = MessengerInterface::TYPE_ERROR;
+ }
+ \Drupal::messenger()->addMessage($message, $status);
+
+ if (\Drupal::service('file_system')->saveData($form_state->getValue('view_css'), static::getViewCSSFilePath(), FileSystemInterface::EXISTS_REPLACE)) {
+ $message = $this->t('View CSS has been written to file.');
+ $status = MessengerInterface::TYPE_STATUS;
+ }
+ else {
+ $message = $this->t('View CSS could not be written to file.');
+ $status = MessengerInterface::TYPE_ERROR;
+ }
+ \Drupal::messenger()->addMessage($message, $status);
+
+ parent::submitForm($form, $form_state);
+ }
+
+ public static function getViewCSSFilePath() {
+ return self::CSS_FILE_DIRECTORY . '/' . self::VIEW_CSS_FILE_NAME;
+ }
+
+ public static function regenerateCSSFile($prefix, $content) {
+ $new_file_name = $prefix . '-' . time() . '.css';
+ /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
+ $file_url_generator = \Drupal::service('file_url_generator');
+ // $css_file_directory_path = $file_url_generator->generateString(self::CSS_FILE_DIRECTORY);
+ $css_file_directory_path = \Drupal::service('file_system')->realpath(self::CSS_FILE_DIRECTORY);
+ $finder = new Finder();
+ $finder->name($prefix . '-*.css');
+ foreach ($finder->in($css_file_directory_path) as $file) {
+ unlink($file->getPathname());
+ }
+
+ if (\Drupal::service('file_system')->saveData($content, self::CSS_FILE_DIRECTORY . '/' . $new_file_name, FileSystemInterface::EXISTS_REPLACE)) {
+ return $new_file_name;
+ }
+ }
+}
\ No newline at end of file
diff --git a/wysiwyg_template.libraries.yml b/wysiwyg_template.libraries.yml
new file mode 100644
index 0000000..f1501cd
--- /dev/null
+++ b/wysiwyg_template.libraries.yml
@@ -0,0 +1,4 @@
+view-template:
+ css:
+ theme:
+ view.css: { type: external }
\ No newline at end of file
diff --git a/wysiwyg_template.links.menu.yml b/wysiwyg_template.links.menu.yml
index 85e1dfc..4bfc512 100644
--- a/wysiwyg_template.links.menu.yml
+++ b/wysiwyg_template.links.menu.yml
@@ -5,3 +5,9 @@ wysiwyg_template.collection:
description: 'Create and modify WYSIWYG templates'
parent: system.admin_config_content
+wysiwyg_template.settings:
+ route_name: entity.wysiwyg_template.settings
+ title: 'Settings'
+ weight: 11
+ description: 'Adjust Wysiwyg Template settings'
+ parent: wysiwyg_template.collection
diff --git a/wysiwyg_template.links.task.yml b/wysiwyg_template.links.task.yml
new file mode 100644
index 0000000..cedc56f
--- /dev/null
+++ b/wysiwyg_template.links.task.yml
@@ -0,0 +1,11 @@
+wysiwyg_template.collection:
+ route_name: entity.wysiwyg_template.collection
+ title: 'WYSIWYG templates'
+ weight: 10
+ base_route: wysiwyg_template.collection
+
+wysiwyg_template.settings:
+ route_name: entity.wysiwyg_template.settings
+ title: 'Settings'
+ weight: 11
+ base_route: wysiwyg_template.collection
diff --git a/wysiwyg_template.module b/wysiwyg_template.module
index fbbe76d..5f5de25 100644
--- a/wysiwyg_template.module
+++ b/wysiwyg_template.module
@@ -5,6 +5,9 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\node\Entity\NodeType;
use Drupal\wysiwyg_template\Entity\Template;
+use Drupal\wysiwyg_template\Form\SettingsForm;
+use Symfony\Component\Finder\Finder;
+use Drupal\Core\File\FileSystemInterface;
/**
* Implements hook_editor_js_settings_alter().
@@ -87,3 +90,89 @@ function wysiwyg_template_form_node_form_alter(array &$form, FormStateInterface
$form['body']['widget'][0]['#default_value'] = $template->getBody();
}
}
+
+/**
+ * Implements hook_ckeditor_css_alter()
+ */
+function wysiwyg_template_ckeditor_css_alter(array &$css, Drupal\editor\Entity\Editor $editor) {
+ /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
+ $file_url_generator = \Drupal::service('file_url_generator');
+
+ $editor_css_file = \Drupal::state()->get('wysiwyg_template.editor_css_file');
+
+ $regenerate_css_file = FALSE;
+
+ if (!empty($editor_css_file)) {
+ $regenerate_css_file = !file_exists(SettingsForm::CSS_FILE_DIRECTORY . '/' . $editor_css_file);
+ }
+ else {
+ $regenerate_css_file = TRUE;
+ }
+ if ($regenerate_css_file) {
+ $editor_css_file = wysiwyg_template_regenerate_css_files('editor_css');
+ \Drupal::state()->set('wysiwyg_template.editor_css_file', $editor_css_file);
+ }
+ $css[] = $file_url_generator->generateString(SettingsForm::CSS_FILE_DIRECTORY . '/' . $editor_css_file);
+}
+
+/**
+ * Implements hook_preprocess_node()
+ */
+function wysiwyg_template_preprocess_node(&$variables) {
+ if (isset($variables['elements']['body'])) {
+ $variables['#attached']['library'][] = 'wysiwyg_template/view-template';
+ }
+}
+
+
+/**
+ * Implements hook_library_info_alter()
+ */
+function wysiwyg_template_library_info_alter(&$libraries, $extension) {
+ if ($extension == 'wysiwyg_template' && isset($libraries['view-template'])) {
+ $libraries['view-template']['css']['theme'] = array(
+ file_create_url(SettingsForm::getViewCSSFilePath()) => array(
+ 'type' => 'external',
+ ),
+ );
+ }
+}
+
+/**
+ * Utility function to call for regenerating CSS files.
+ */
+function wysiwyg_template_regenerate_css_files($type) {
+ $config = \Drupal::config('wysiwyg_template.settings');
+ if ($type == 'editor_css') {
+ $editor_css_file = SettingsForm::regenerateCSSFile('editor', $config->get($type));
+ return \Drupal::state()->set('wysiwyg_template.editor_css_file', $editor_css_file);
+ }
+ else if ($type == 'view_css') {
+ return \Drupal::service('file_system')->saveData($config->get($type), SettingsForm::getViewCSSFilePath(), FileSystemInterface::EXISTS_REPLACE);
+ }
+}
+
+
+/**
+ * Implements hook_cache_flush()
+ */
+function bwma_cache_flush() {
+ $dir = \Drupal::service('file_system')->realpath('public://mce/');
+
+ $finder = new Finder();
+ $finder->name('*.css');
+ $finder->files()->in($dir);
+
+ foreach ($finder as $file) {
+ $absoluteFilePath = $file->getRealPath();
+ unlink($absoluteFilePath);
+ }
+}
+
+/**
+ * Implements hook_rebuild()
+ */
+function bwma_rebuild() {
+ wysiwyg_template_regenerate_css_files('editor_css');
+ wysiwyg_template_regenerate_css_files('view_css');
+}
diff --git a/wysiwyg_template.routing.yml b/wysiwyg_template.routing.yml
index 85a0a4a..d5c508d 100644
--- a/wysiwyg_template.routing.yml
+++ b/wysiwyg_template.routing.yml
@@ -49,3 +49,11 @@ entity.wysiwyg_template.delete_form:
_title: 'Delete template'
requirements:
_permission: 'administer wysiwyg templates'
+
+entity.wysiwyg_template.settings:
+ path: '/admin/config/content/wysiwyg-templates/settings'
+ defaults:
+ _form: '\Drupal\wysiwyg_template\Form\SettingsForm'
+ _title: 'Wysiwyg Template Settings'
+ requirements:
+ _permission: 'administer wysiwyg templates'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment