You are here

function ckeditor_templates_ui_js_settings_alter in Ckeditor templates user interface 8

Implements hook_js_settings_alter().

File

./ckeditor_templates_ui.module, line 47
Ckeditor_templates_ui module file.

Code

function ckeditor_templates_ui_js_settings_alter(array &$settings, AttachedAssetsInterface $assets) {
  $libraries = $assets
    ->getLibraries();
  if (in_array('ckeditor_templates/ckeditor.templates.dialog', $libraries, TRUE)) {
    $query = \Drupal::entityTypeManager()
      ->getStorage('ckeditor_template')
      ->getQuery();
    $templates_ids = $query
      ->execute();
    $ckeditor_templates = \Drupal::entityTypeManager()
      ->getStorage('ckeditor_template')
      ->loadMultiple($templates_ids);

    // Sorting in postLoad does not stick.
    uasort($ckeditor_templates, 'Drupal\\Core\\Config\\Entity\\ConfigEntityBase::sort');
    $i = 0;
    $templates = [];
    foreach ($ckeditor_templates as $value) {
      $templates[$i]['title'] = $value->label;
      $templates[$i]['description'] = $value->description;
      if ($value->image) {

        // The CKEditor Templates plugin requires a "imagesPath" parameter
        // that cannot evaluate to false, is the same for all templates and
        // is used to create the image path. This makes it inconvenient for us.
        // For things to work out all url-s must start with "/".
        $templates[$i]['image'] = file_create_url($value->image);
        $templates[$i]['image'] = file_url_transform_relative($templates[$i]['image']);

        // Remove leading "/" since it will be added separately
        // by the "imagesPath" parameter.
        $templates[$i]['image'] = substr($templates[$i]['image'], 1);
      }
      $templates[$i]['html'] = $value->html['value'];
      $i++;
    }
    $settings['ckeditor_templates_ui']['templates'] = $templates;
  }
}