You are here

protected function Internal::generateFormatTagsSetting in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/ckeditor/src/Plugin/CKEditorPlugin/Internal.php \Drupal\ckeditor\Plugin\CKEditorPlugin\Internal::generateFormatTagsSetting()
  2. 10 core/modules/ckeditor/src/Plugin/CKEditorPlugin/Internal.php \Drupal\ckeditor\Plugin\CKEditorPlugin\Internal::generateFormatTagsSetting()

Builds the "format_tags" configuration part of the CKEditor JS settings.

Parameters

\Drupal\editor\Entity\Editor $editor: A configured text editor object.

Return value

array An array containing the "format_tags" configuration.

See also

getConfig()

1 call to Internal::generateFormatTagsSetting()
Internal::getConfig in core/modules/ckeditor/src/Plugin/CKEditorPlugin/Internal.php
Returns the additions to CKEDITOR.config for a specific CKEditor instance.

File

core/modules/ckeditor/src/Plugin/CKEditorPlugin/Internal.php, line 350

Class

Internal
Defines the "internal" plugin (i.e. core plugins part of our CKEditor build).

Namespace

Drupal\ckeditor\Plugin\CKEditorPlugin

Code

protected function generateFormatTagsSetting(Editor $editor) {

  // When no text format is associated yet, assume no tag is allowed.
  // @see \Drupal\editor\EditorInterface::hasAssociatedFilterFormat()
  if (!$editor
    ->hasAssociatedFilterFormat()) {
    return [];
  }
  $format = $editor
    ->getFilterFormat();
  $cid = 'ckeditor_internal_format_tags:' . $format
    ->id();
  if ($cached = $this->cache
    ->get($cid)) {
    $format_tags = $cached->data;
  }
  else {

    // The <p> tag is always allowed — HTML without <p> tags is nonsensical.
    $format_tags = [
      'p',
    ];

    // Given the list of possible format tags, automatically determine whether
    // the current text format allows this tag, and thus whether it should show
    // up in the "Format" dropdown.
    $possible_format_tags = [
      'h1',
      'h2',
      'h3',
      'h4',
      'h5',
      'h6',
      'pre',
    ];
    foreach ($possible_format_tags as $tag) {
      $input = '<' . $tag . '>TEST</' . $tag . '>';
      $output = trim(check_markup($input, $editor
        ->id()));
      if (Html::load($output)
        ->getElementsByTagName($tag)->length !== 0) {
        $format_tags[] = $tag;
      }
    }
    $format_tags = implode(';', $format_tags);

    // Cache the "format_tags" configuration. This cache item is infinitely
    // valid; it only changes whenever the text format is changed, hence it's
    // tagged with the text format's cache tag.
    $this->cache
      ->set($cid, $format_tags, Cache::PERMANENT, $format
      ->getCacheTags());
  }
  return $format_tags;
}