class Element

Same name in this branch
  1. 10 core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element
  2. 10 core/lib/Drupal/Core/Config/Schema/Element.php \Drupal\Core\Config\Schema\Element
Same name in other branches
  1. 9 core/modules/editor/src/Element.php \Drupal\editor\Element
  2. 9 core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element
  3. 9 core/lib/Drupal/Core/Config/Schema/Element.php \Drupal\Core\Config\Schema\Element
  4. 8.9.x core/modules/editor/src/Element.php \Drupal\editor\Element
  5. 8.9.x core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element
  6. 8.9.x core/lib/Drupal/Core/Config/Schema/Element.php \Drupal\Core\Config\Schema\Element
  7. 11.x core/modules/editor/src/Element.php \Drupal\editor\Element
  8. 11.x core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element
  9. 11.x core/lib/Drupal/Core/Config/Schema/Element.php \Drupal\Core\Config\Schema\Element

Defines a service for Text Editor's render elements.

Hierarchy

Expanded class hierarchy of Element

23 string references to 'Element'
ckeditor5.data_types.yml in core/modules/ckeditor5/config/schema/ckeditor5.data_types.yml
core/modules/ckeditor5/config/schema/ckeditor5.data_types.yml
ckeditor5_filter_format_edit_form_submit in core/modules/ckeditor5/ckeditor5.module
Form submission handler for filter format forms.
config_translation_theme in core/modules/config_translation/config_translation.module
Implements hook_theme().
drupal_common_theme in core/includes/theme.inc
Provides theme registration for themes across .inc files.
editor.services.yml in core/modules/editor/editor.services.yml
core/modules/editor/editor.services.yml

... See full list

1 service uses Element
element.editor in core/modules/editor/editor.services.yml
Drupal\editor\Element

File

core/modules/editor/src/Element.php, line 14

Namespace

Drupal\editor
View source
class Element implements TrustedCallbackInterface {
    
    /**
     * The Text Editor plugin manager service.
     *
     * @var \Drupal\Component\Plugin\PluginManagerInterface
     */
    protected $pluginManager;
    
    /**
     * Constructs a new Element object.
     *
     * @param \Drupal\Component\Plugin\PluginManagerInterface $plugin_manager
     *   The Text Editor plugin manager service.
     */
    public function __construct(PluginManagerInterface $plugin_manager) {
        $this->pluginManager = $plugin_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function trustedCallbacks() {
        return [
            'preRenderTextFormat',
        ];
    }
    
    /**
     * Additional #pre_render callback for 'text_format' elements.
     */
    public function preRenderTextFormat(array $element) {
        // Allow modules to programmatically enforce no client-side editor by
        // setting the #editor property to FALSE.
        if (isset($element['#editor']) && !$element['#editor']) {
            return $element;
        }
        // \Drupal\filter\Element\TextFormat::processFormat() copies properties to
        // the expanded 'value' to the child element, including the #pre_render
        // property. Skip this text format widget, if it contains no 'format'.
        if (!isset($element['format'])) {
            return $element;
        }
        $format_ids = array_keys($element['format']['format']['#options']);
        // Early-return if no text editor is associated with any of the text formats.
        $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;
        }
        // Use a hidden element for a single text format.
        $field_id = $element['value']['#id'];
        if (!$element['format']['format']['#access']) {
            // Use the first (and only) available text format.
            $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;
        }
        // Hide the text format's filters' guidelines of those text formats that have
        // a text editor associated: they're rather useless when using a text editor.
        foreach ($editors as $format_id => $editor) {
            $element['format']['guidelines'][$format_id]['#access'] = FALSE;
        }
        // Attach Text Editor module's (this module) library.
        $element['#attached']['library'][] = 'editor/drupal.editor';
        // Attach attachments for all available editors.
        $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached'], $this->pluginManager
            ->getAttachments($format_ids));
        // Apply XSS filters when editing content if necessary. Some types of text
        // editors cannot guarantee that the end user won't become a victim of XSS.
        if (!empty($element['value']['#value'])) {
            $original = $element['value']['#value'];
            $format = FilterFormat::load($element['format']['format']['#value']);
            // Ensure XSS-safety for the current text format/editor.
            $filtered = editor_filter_xss($original, $format);
            if ($filtered !== FALSE) {
                $element['value']['#value'] = $filtered;
            }
            // Only when the user has access to multiple text formats, we must add data-
            // attributes for the original value and change tracking, because they are
            // only necessary when the end user can switch between text formats/editors.
            if ($element['format']['format']['#access']) {
                $element['value']['#attributes']['data-editor-value-is-changed'] = 'false';
                $element['value']['#attributes']['data-editor-value-original'] = $original;
            }
        }
        return $element;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
Element::$pluginManager protected property The Text Editor plugin manager service.
Element::preRenderTextFormat public function Additional #pre_render callback for 'text_format' elements.
Element::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
Element::__construct public function Constructs a new Element object.
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING Deprecated constant Untrusted callbacks trigger E_USER_WARNING errors.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.