Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / editor / src / Element.php
1 <?php
2
3 namespace Drupal\editor;
4
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\Component\Plugin\PluginManagerInterface;
8 use Drupal\Core\Render\BubbleableMetadata;
9
10 /**
11  * Defines a service for Text Editor's render elements.
12  */
13 class Element {
14
15   /**
16    * The Text Editor plugin manager service.
17    *
18    * @var \Drupal\Component\Plugin\PluginManagerInterface
19    */
20   protected $pluginManager;
21
22   /**
23    * Constructs a new Element object.
24    *
25    * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
26    *   The Text Editor plugin manager service.
27    */
28   public function __construct(PluginManagerInterface $plugin_manager) {
29     $this->pluginManager = $plugin_manager;
30   }
31
32   /**
33    * Additional #pre_render callback for 'text_format' elements.
34    */
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']) {
39       return $element;
40     }
41
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'])) {
46       return $element;
47     }
48     $format_ids = array_keys($element['format']['format']['#options']);
49
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]);
56       }
57     }
58     if (count($editors) === 0) {
59       return $element;
60     }
61
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'] = [
68         '#type' => 'hidden',
69         '#name' => $element['format']['format']['#name'],
70         '#value' => $format_id,
71         '#attributes' => [
72           'data-editor-for' => $field_id,
73         ],
74       ];
75     }
76     // Otherwise, attach to text format selector.
77     else {
78       $element['format']['format']['#attributes']['class'][] = 'editor';
79       $element['format']['format']['#attributes']['data-editor-for'] = $field_id;
80     }
81
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;
86     }
87
88     // Attach Text Editor module's (this module) library.
89     $element['#attached']['library'][] = 'editor/drupal.editor';
90
91     // Attach attachments for all available editors.
92     $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached'], $this->pluginManager->getAttachments($format_ids));
93
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']);
99
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;
104       }
105
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;
112       }
113     }
114
115     return $element;
116   }
117
118 }