Element.php in Drupal 8
File
core/modules/editor/src/Element.php
View source
<?php
namespace Drupal\editor;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Render\BubbleableMetadata;
class Element implements TrustedCallbackInterface {
protected $pluginManager;
public function __construct(PluginManagerInterface $plugin_manager) {
$this->pluginManager = $plugin_manager;
}
public static function trustedCallbacks() {
return [
'preRenderTextFormat',
];
}
public function preRenderTextFormat(array $element) {
if (isset($element['#editor']) && !$element['#editor']) {
return $element;
}
if (!isset($element['format'])) {
return $element;
}
$format_ids = array_keys($element['format']['format']['#options']);
$editors = Editor::loadMultiple($format_ids);
foreach ($editors as $key => $editor) {
$definition = $this->pluginManager
->getDefinition($editor
->getEditor());
if (!in_array($element['#base_type'], $definition['supported_element_types'])) {
unset($editors[$key]);
}
}
if (count($editors) === 0) {
return $element;
}
$field_id = $element['value']['#id'];
if (!$element['format']['format']['#access']) {
$format_id = $format_ids[0];
$element['format']['editor'] = [
'#type' => 'hidden',
'#name' => $element['format']['format']['#name'],
'#value' => $format_id,
'#attributes' => [
'data-editor-for' => $field_id,
],
];
}
else {
$element['format']['format']['#attributes']['class'][] = 'editor';
$element['format']['format']['#attributes']['data-editor-for'] = $field_id;
}
foreach ($editors as $format_id => $editor) {
$element['format']['guidelines'][$format_id]['#access'] = FALSE;
}
$element['#attached']['library'][] = 'editor/drupal.editor';
$element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached'], $this->pluginManager
->getAttachments($format_ids));
if (!empty($element['value']['#value'])) {
$original = $element['value']['#value'];
$format = FilterFormat::load($element['format']['format']['#value']);
$filtered = editor_filter_xss($original, $format);
if ($filtered !== FALSE) {
$element['value']['#value'] = $filtered;
}
if ($element['format']['format']['#access']) {
$element['value']['#attributes']['data-editor-value-is-changed'] = 'false';
$element['value']['#attributes']['data-editor-value-original'] = $original;
}
}
return $element;
}
}
Classes
Name |
Description |
Element |
Defines a service for Text Editor's render elements. |