You are here

codesnippet.module in CKEditor CodeSnippet 8

Provides integration with the CKEditor WYSIWYG editor.

File

codesnippet.module
View source
<?php

/**
 * @file
 * Provides integration with the CKEditor WYSIWYG editor.
 */
use Drupal\editor\Entity\Editor;

/**
 * Implements hook_ckeditor_css_alter().
 *
 * Injects our selected CSS sheet anytime CKEditor has loaded.
 */
function codesnippet_ckeditor_css_alter(array &$css, Editor $editor) {
  if (!$editor
    ->hasAssociatedFilterFormat()) {
    return;
  }
  $settings = $editor
    ->getSettings();
  if (!empty($settings['plugins']['codesnippet']['highlight_style'])) {
    $css[] = 'libraries/codesnippet/lib/highlight/styles/' . $settings['plugins']['codesnippet']['highlight_style'] . '.css';
  }
}

/**
 * Implements hook_preprocess_html().
 *
 * Ensure our necessary scripts are loaded to the page.
 */
function codesnippet_preprocess_html(&$variables) {
  $variables['#attached']['library'][] = 'codesnippet/codesnippet.highlightjs';
  $entity = \Drupal::entityTypeManager()
    ->getStorage('editor');
  $editors = $entity
    ->loadMultiple();
  foreach ($editors as $editor) {
    if ($editor
      ->getEditor() != 'ckeditor') {

      // bail, as the plugin only works for CKEditor.
      continue;
    }
    $settings = $editor
      ->getSettings();
    if (!empty($settings['plugins']['codesnippet']['highlight_style'])) {
      $variables['#attached']['library'][] = 'codesnippet/codesnippet.style.' . $settings['plugins']['codesnippet']['highlight_style'];
    }
  }
}

Functions

Namesort descending Description
codesnippet_ckeditor_css_alter Implements hook_ckeditor_css_alter().
codesnippet_preprocess_html Implements hook_preprocess_html().