Chris@0: pluginManager = $plugin_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Additional #pre_render callback for 'text_format' elements. Chris@0: */ Chris@0: public function preRenderTextFormat(array $element) { Chris@0: // Allow modules to programmatically enforce no client-side editor by Chris@0: // setting the #editor property to FALSE. Chris@0: if (isset($element['#editor']) && !$element['#editor']) { Chris@0: return $element; Chris@0: } Chris@0: Chris@0: // filter_process_format() copies properties to the expanded 'value' child Chris@0: // element, including the #pre_render property. Skip this text format Chris@0: // widget, if it contains no 'format'. Chris@0: if (!isset($element['format'])) { Chris@0: return $element; Chris@0: } Chris@0: $format_ids = array_keys($element['format']['format']['#options']); Chris@0: Chris@0: // Early-return if no text editor is associated with any of the text formats. Chris@0: $editors = Editor::loadMultiple($format_ids); Chris@0: foreach ($editors as $key => $editor) { Chris@0: $definition = $this->pluginManager->getDefinition($editor->getEditor()); Chris@0: if (!in_array($element['#base_type'], $definition['supported_element_types'])) { Chris@0: unset($editors[$key]); Chris@0: } Chris@0: } Chris@0: if (count($editors) === 0) { Chris@0: return $element; Chris@0: } Chris@0: Chris@0: // Use a hidden element for a single text format. Chris@0: $field_id = $element['value']['#id']; Chris@0: if (!$element['format']['format']['#access']) { Chris@0: // Use the first (and only) available text format. Chris@0: $format_id = $format_ids[0]; Chris@0: $element['format']['editor'] = [ Chris@0: '#type' => 'hidden', Chris@0: '#name' => $element['format']['format']['#name'], Chris@0: '#value' => $format_id, Chris@0: '#attributes' => [ Chris@0: 'data-editor-for' => $field_id, Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: // Otherwise, attach to text format selector. Chris@0: else { Chris@0: $element['format']['format']['#attributes']['class'][] = 'editor'; Chris@0: $element['format']['format']['#attributes']['data-editor-for'] = $field_id; Chris@0: } Chris@0: Chris@0: // Hide the text format's filters' guidelines of those text formats that have Chris@0: // a text editor associated: they're rather useless when using a text editor. Chris@0: foreach ($editors as $format_id => $editor) { Chris@0: $element['format']['guidelines'][$format_id]['#access'] = FALSE; Chris@0: } Chris@0: Chris@0: // Attach Text Editor module's (this module) library. Chris@0: $element['#attached']['library'][] = 'editor/drupal.editor'; Chris@0: Chris@0: // Attach attachments for all available editors. Chris@0: $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached'], $this->pluginManager->getAttachments($format_ids)); Chris@0: Chris@0: // Apply XSS filters when editing content if necessary. Some types of text Chris@0: // editors cannot guarantee that the end user won't become a victim of XSS. Chris@0: if (!empty($element['value']['#value'])) { Chris@0: $original = $element['value']['#value']; Chris@0: $format = FilterFormat::load($element['format']['format']['#value']); Chris@0: Chris@0: // Ensure XSS-safety for the current text format/editor. Chris@0: $filtered = editor_filter_xss($original, $format); Chris@0: if ($filtered !== FALSE) { Chris@0: $element['value']['#value'] = $filtered; Chris@0: } Chris@0: Chris@0: // Only when the user has access to multiple text formats, we must add data- Chris@0: // attributes for the original value and change tracking, because they are Chris@0: // only necessary when the end user can switch between text formats/editors. Chris@0: if ($element['format']['format']['#access']) { Chris@0: $element['value']['#attributes']['data-editor-value-is-changed'] = 'false'; Chris@0: $element['value']['#attributes']['data-editor-value-original'] = $original; Chris@0: } Chris@0: } Chris@0: Chris@0: return $element; Chris@0: } Chris@0: Chris@0: }