Exemplo n.º 1
0
 /**
  * Builds the "format_tags" configuration part of the CKEditor JS settings.
  *
  * @see getConfig()
  *
  * @param \Drupal\editor\Entity\Editor $editor
  *   A configured text editor object.
  *
  * @return array
  *   An array containing the "format_tags" configuration.
  */
 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 array();
     }
     $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 ($input == $output) {
                 $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;
 }