3 namespace Drupal\editor;
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\Component\Plugin\PluginManagerInterface;
8 use Drupal\Core\Render\BubbleableMetadata;
11 * Defines a service for Text Editor's render elements.
16 * The Text Editor plugin manager service.
18 * @var \Drupal\Component\Plugin\PluginManagerInterface
20 protected $pluginManager;
23 * Constructs a new Element object.
25 * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
26 * The Text Editor plugin manager service.
28 public function __construct(PluginManagerInterface $plugin_manager) {
29 $this->pluginManager = $plugin_manager;
33 * Additional #pre_render callback for 'text_format' elements.
35 public function preRenderTextFormat(array $element) {
36 // Allow modules to programmatically enforce no client-side editor by
37 // setting the #editor property to FALSE.
38 if (isset($element['#editor']) && !$element['#editor']) {
42 // filter_process_format() copies properties to the expanded 'value' child
43 // element, including the #pre_render property. Skip this text format
44 // widget, if it contains no 'format'.
45 if (!isset($element['format'])) {
48 $format_ids = array_keys($element['format']['format']['#options']);
50 // Early-return if no text editor is associated with any of the text formats.
51 $editors = Editor::loadMultiple($format_ids);
52 foreach ($editors as $key => $editor) {
53 $definition = $this->pluginManager->getDefinition($editor->getEditor());
54 if (!in_array($element['#base_type'], $definition['supported_element_types'])) {
55 unset($editors[$key]);
58 if (count($editors) === 0) {
62 // Use a hidden element for a single text format.
63 $field_id = $element['value']['#id'];
64 if (!$element['format']['format']['#access']) {
65 // Use the first (and only) available text format.
66 $format_id = $format_ids[0];
67 $element['format']['editor'] = [
69 '#name' => $element['format']['format']['#name'],
70 '#value' => $format_id,
72 'data-editor-for' => $field_id,
76 // Otherwise, attach to text format selector.
78 $element['format']['format']['#attributes']['class'][] = 'editor';
79 $element['format']['format']['#attributes']['data-editor-for'] = $field_id;
82 // Hide the text format's filters' guidelines of those text formats that have
83 // a text editor associated: they're rather useless when using a text editor.
84 foreach ($editors as $format_id => $editor) {
85 $element['format']['guidelines'][$format_id]['#access'] = FALSE;
88 // Attach Text Editor module's (this module) library.
89 $element['#attached']['library'][] = 'editor/drupal.editor';
91 // Attach attachments for all available editors.
92 $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached'], $this->pluginManager->getAttachments($format_ids));
94 // Apply XSS filters when editing content if necessary. Some types of text
95 // editors cannot guarantee that the end user won't become a victim of XSS.
96 if (!empty($element['value']['#value'])) {
97 $original = $element['value']['#value'];
98 $format = FilterFormat::load($element['format']['format']['#value']);
100 // Ensure XSS-safety for the current text format/editor.
101 $filtered = editor_filter_xss($original, $format);
102 if ($filtered !== FALSE) {
103 $element['value']['#value'] = $filtered;
106 // Only when the user has access to multiple text formats, we must add data-
107 // attributes for the original value and change tracking, because they are
108 // only necessary when the end user can switch between text formats/editors.
109 if ($element['format']['format']['#access']) {
110 $element['value']['#attributes']['data-editor-value-is-changed'] = 'false';
111 $element['value']['#attributes']['data-editor-value-original'] = $original;