class Element

Same name in this branch
  1. 9 core/modules/editor/src/Element.php \Drupal\editor\Element
  2. 9 core/lib/Drupal/Core/Config/Schema/Element.php \Drupal\Core\Config\Schema\Element
Same name and namespace in other branches
  1. 8.9.x core/modules/editor/src/Element.php \Drupal\editor\Element
  2. 8.9.x core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element
  3. 8.9.x core/lib/Drupal/Core/Config/Schema/Element.php \Drupal\Core\Config\Schema\Element
  4. 10 core/modules/editor/src/Element.php \Drupal\editor\Element
  5. 10 core/lib/Drupal/Core/Render/Element.php \Drupal\Core\Render\Element
  6. 10 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

Provides helper methods for Drupal render elements.

Hierarchy

Expanded class hierarchy of Element

See also

\Drupal\Core\Render\Element\ElementInterface

Related topics

108 files declare their use of Element
AccountSettingsForm.php in core/modules/user/src/AccountSettingsForm.php
Actions.php in core/lib/Drupal/Core/Render/Element/Actions.php
AddFormBase.php in core/modules/media_library/src/Form/AddFormBase.php
aggregator.theme.inc in core/modules/aggregator/aggregator.theme.inc
Preprocessors and theme functions of Aggregator module.
ArgumentPluginBase.php in core/modules/views/src/Plugin/views/argument/ArgumentPluginBase.php

... See full list

21 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.
ElementInfoManager::__construct in core/lib/Drupal/Core/Render/ElementInfoManager.php
Constructs an ElementInfoManager object.

... See full list

File

core/lib/Drupal/Core/Render/Element.php, line 15

Namespace

Drupal\Core\Render
View source
class Element {
    
    /**
     * Checks if the key is a property.
     *
     * @param string $key
     *   The key to check.
     *
     * @return bool
     *   TRUE of the key is a property, FALSE otherwise.
     */
    public static function property($key) {
        return is_string($key) && $key[0] == '#';
    }
    
    /**
     * Gets properties of a structured array element (keys beginning with '#').
     *
     * @param array $element
     *   An element array to return properties for.
     *
     * @return array
     *   An array of property keys for the element.
     */
    public static function properties(array $element) {
        return array_filter(array_keys($element), 'static::property');
    }
    
    /**
     * Checks if the key is a child.
     *
     * @param string $key
     *   The key to check.
     *
     * @return bool
     *   TRUE if the element is a child, FALSE otherwise.
     */
    public static function child($key) {
        return !isset($key[0]) || $key[0] != '#';
    }
    
    /**
     * Identifies the children of an element array, optionally sorted by weight.
     *
     * The children of an element array are those key/value pairs whose key does
     * not start with a '#'. See \Drupal\Core\Render\RendererInterface::render()
     * for details.
     *
     * @param array $elements
     *   The element array whose children are to be identified. Passed by
     *   reference.
     * @param bool $sort
     *   Boolean to indicate whether the children should be sorted by weight.
     *
     * @return array
     *   The array keys of the element's children.
     */
    public static function children(array &$elements, $sort = FALSE) {
        // Do not attempt to sort elements which have already been sorted.
        $sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
        // Filter out properties from the element, leaving only children.
        $count = count($elements);
        $child_weights = [];
        $i = 0;
        $sortable = FALSE;
        foreach ($elements as $key => $value) {
            if (is_int($key) || $key === '' || $key[0] !== '#') {
                if (is_array($value)) {
                    if (isset($value['#weight'])) {
                        $weight = $value['#weight'];
                        $sortable = TRUE;
                    }
                    else {
                        $weight = 0;
                    }
                    // Supports weight with up to three digit precision and conserve
                    // the insertion order.
                    $child_weights[$key] = floor($weight * 1000) + $i / $count;
                }
                elseif (isset($value)) {
                    trigger_error(new FormattableMarkup('"@key" is an invalid render array key', [
                        '@key' => $key,
                    ]), E_USER_ERROR);
                }
            }
            $i++;
        }
        // Sort the children if necessary.
        if ($sort && $sortable) {
            asort($child_weights);
            // Put the sorted children back into $elements in the correct order, to
            // preserve sorting if the same element is passed through
            // \Drupal\Core\Render\Element::children() twice.
            foreach ($child_weights as $key => $weight) {
                $value = $elements[$key];
                unset($elements[$key]);
                $elements[$key] = $value;
            }
            $elements['#sorted'] = TRUE;
        }
        return array_keys($child_weights);
    }
    
    /**
     * Returns the visible children of an element.
     *
     * @param array $elements
     *   The parent element.
     *
     * @return array
     *   The array keys of the element's visible children.
     */
    public static function getVisibleChildren(array $elements) {
        $visible_children = [];
        foreach (static::children($elements) as $key) {
            $child = $elements[$key];
            // Skip value and hidden elements, since they are not rendered.
            if (!static::isVisibleElement($child)) {
                continue;
            }
            $visible_children[$key] = $child;
        }
        return array_keys($visible_children);
    }
    
    /**
     * Determines if an element is visible.
     *
     * @param array $element
     *   The element to check for visibility.
     *
     * @return bool
     *   TRUE if the element is visible, otherwise FALSE.
     */
    public static function isVisibleElement($element) {
        return (!isset($element['#type']) || !in_array($element['#type'], [
            'value',
            'hidden',
            'token',
        ])) && (!isset($element['#access']) || ($element['#access'] instanceof AccessResultInterface && $element['#access']->isAllowed() || $element['#access'] === TRUE));
    }
    
    /**
     * Sets HTML attributes based on element properties.
     *
     * @param array $element
     *   The renderable element to process. Passed by reference.
     * @param array $map
     *   An associative array whose keys are element property names and whose
     *   values are the HTML attribute names to set on the corresponding
     *   property; e.g., array('#property_name' => 'attribute_name'). If both
     *   names are identical except for the leading '#', then an attribute name
     *   value is sufficient and no property name needs to be specified.
     */
    public static function setAttributes(array &$element, array $map) {
        foreach ($map as $property => $attribute) {
            // If the key is numeric, the attribute name needs to be taken over.
            if (is_int($property)) {
                $property = '#' . $attribute;
            }
            // Do not overwrite already existing attributes.
            if (isset($element[$property]) && !isset($element['#attributes'][$attribute])) {
                $element['#attributes'][$attribute] = $element[$property];
            }
        }
    }
    
    /**
     * Indicates whether the given element is empty.
     *
     * An element that only has #cache or #weight set is considered
     * empty, because it will render to the empty string.
     *
     * @param array $elements
     *   The element.
     *
     * @return bool
     *   Whether the given element is empty.
     */
    public static function isEmpty(array $elements) {
        return \array_diff(\array_keys($elements), [
            '#cache',
            '#weight',
        ]) === [];
    }

}

Members

Title Sort descending Modifiers Object type Summary
Element::child public static function Checks if the key is a child.
Element::children public static function Identifies the children of an element array, optionally sorted by weight.
Element::getVisibleChildren public static function Returns the visible children of an element.
Element::isEmpty public static function Indicates whether the given element is empty.
Element::isVisibleElement public static function Determines if an element is visible.
Element::properties public static function Gets properties of a structured array element (keys beginning with '#').
Element::property public static function Checks if the key is a property.
Element::setAttributes public static function Sets HTML attributes based on element properties.

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