3 namespace Drupal\Core\Render;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\Core\Access\AccessResultInterface;
9 * Provides helper methods for Drupal render elements.
11 * @see \Drupal\Core\Render\Element\ElementInterface
13 * @ingroup theme_render
18 * Checks if the key is a property.
24 * TRUE of the key is a property, FALSE otherwise.
26 public static function property($key) {
27 return $key[0] == '#';
31 * Gets properties of a structured array element (keys beginning with '#').
33 * @param array $element
34 * An element array to return properties for.
37 * An array of property keys for the element.
39 public static function properties(array $element) {
40 return array_filter(array_keys($element), 'static::property');
44 * Checks if the key is a child.
50 * TRUE if the element is a child, FALSE otherwise.
52 public static function child($key) {
53 return !isset($key[0]) || $key[0] != '#';
57 * Identifies the children of an element array, optionally sorted by weight.
59 * The children of a element array are those key/value pairs whose key does
60 * not start with a '#'. See drupal_render() for details.
62 * @param array $elements
63 * The element array whose children are to be identified. Passed by
66 * Boolean to indicate whether the children should be sorted by weight.
69 * The array keys of the element's children.
71 public static function children(array &$elements, $sort = FALSE) {
72 // Do not attempt to sort elements which have already been sorted.
73 $sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
75 // Filter out properties from the element, leaving only children.
76 $count = count($elements);
80 foreach ($elements as $key => $value) {
81 if ($key === '' || $key[0] !== '#') {
82 if (is_array($value)) {
83 if (isset($value['#weight'])) {
84 $weight = $value['#weight'];
90 // Supports weight with up to three digit precision and conserve
91 // the insertion order.
92 $child_weights[$key] = floor($weight * 1000) + $i / $count;
94 // Only trigger an error if the value is not null.
95 // @see https://www.drupal.org/node/1283892
96 elseif (isset($value)) {
97 trigger_error(SafeMarkup::format('"@key" is an invalid render array key', ['@key' => $key]), E_USER_ERROR);
103 // Sort the children if necessary.
104 if ($sort && $sortable) {
105 asort($child_weights);
106 // Put the sorted children back into $elements in the correct order, to
107 // preserve sorting if the same element is passed through
108 // \Drupal\Core\Render\Element::children() twice.
109 foreach ($child_weights as $key => $weight) {
110 $value = $elements[$key];
111 unset($elements[$key]);
112 $elements[$key] = $value;
114 $elements['#sorted'] = TRUE;
117 return array_keys($child_weights);
121 * Returns the visible children of an element.
123 * @param array $elements
124 * The parent element.
127 * The array keys of the element's visible children.
129 public static function getVisibleChildren(array $elements) {
130 $visible_children = [];
132 foreach (static::children($elements) as $key) {
133 $child = $elements[$key];
135 // Skip value and hidden elements, since they are not rendered.
136 if (!static::isVisibleElement($child)) {
140 $visible_children[$key] = $child;
143 return array_keys($visible_children);
147 * Determines if an element is visible.
149 * @param array $element
150 * The element to check for visibility.
153 * TRUE if the element is visible, otherwise FALSE.
155 public static function isVisibleElement($element) {
156 return (!isset($element['#type']) || !in_array($element['#type'], ['value', 'hidden', 'token']))
157 && (!isset($element['#access'])
158 || (($element['#access'] instanceof AccessResultInterface && $element['#access']->isAllowed()) || ($element['#access'] === TRUE)));
162 * Sets HTML attributes based on element properties.
164 * @param array $element
165 * The renderable element to process. Passed by reference.
167 * An associative array whose keys are element property names and whose
168 * values are the HTML attribute names to set on the corresponding
169 * property; e.g., array('#propertyname' => 'attributename'). If both names
170 * are identical except for the leading '#', then an attribute name value is
171 * sufficient and no property name needs to be specified.
173 public static function setAttributes(array &$element, array $map) {
174 foreach ($map as $property => $attribute) {
175 // If the key is numeric, the attribute name needs to be taken over.
176 if (is_int($property)) {
177 $property = '#' . $attribute;
179 // Do not overwrite already existing attributes.
180 if (isset($element[$property]) && !isset($element['#attributes'][$attribute])) {
181 $element['#attributes'][$attribute] = $element[$property];
187 * Indicates whether the given element is empty.
189 * An element that only has #cache set is considered empty, because it will
190 * render to the empty string.
192 * @param array $elements
196 * Whether the given element is empty.
198 public static function isEmpty(array $elements) {
199 return empty($elements) || (count($elements) === 1 && array_keys($elements) === ['#cache']);