Skip to content

RenderElement

Utility helpers for working with render elements.


  • Full name: \Drupal\helper\RenderElement

Methods

wrap

Add a wrapper around a render element.

public static wrap(array& $element, string|\Drupal\Component\Render\MarkupInterface $prefix, string|\Drupal\Component\Render\MarkupInterface $suffix, bool $inside = false): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$prefix string|\Drupal\Component\Render\MarkupInterface
The prefix to add to the element.
$suffix string|\Drupal\Component\Render\MarkupInterface
The suffix to add to the element.
$inside bool
If the wrapper should be inside the existing prefix and suffix.

applyToChildren

Apply a callback to the render element children.

public static applyToChildren(array& $element, callable $callback, mixed $args): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$callback callable
The callback to apply.
$args mixed
The additional parameters to provide to the callback.

attachLibrary

Attach a library to the render element.

public static attachLibrary(array& $element, string $library): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$library string
The library, like 'your_module/library_name'.

See Also:

  • https://www.drupal.org/docs/creating-modules/adding-assets-css-js-to-a-drupal-module-via-librariesyml#render-array

attachDrupalSetting

Attach a single drupalSettings key/value.

public static attachDrupalSetting(array& $element, string $key, mixed $value): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$key string
The drupalSettings key.
$value mixed
The drupalSettings value.

See Also:

  • https://www.drupal.org/docs/creating-modules/adding-assets-css-js-to-a-drupal-module-via-librariesyml#render-array
  • https://www.drupal.org/node/2274843#configurable

attachDrupalSettings

Attach multiple drupalSettings.

public static attachDrupalSettings(array& $element, array $settings): void

The settings are merged deeply with any existing settings, the same as when core merges the attachments of render elements. Values in $settings replace existing values with the same keys.

  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$settings array
The drupalSettings key/value array.

See Also:

  • https://www.drupal.org/docs/creating-modules/adding-assets-css-js-to-a-drupal-module-via-librariesyml#render-array
  • https://www.drupal.org/node/2274843#configurable

attachHeader

Attach a HTML header.

public static attachHeader(array& $element, string $name, string $value, bool $replace = false): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$name string
The header name, 'status' is treated specially and used as http status
code.
$value string
The header value.
$replace bool
(optional) Whether to replace a current value with the new one, or add
it to the others. If the value is not replaced, it will be appended,
resulting in a header like this: 'Header: value1,value2'.

See Also:

  • \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::setHeaders

attachFeed

Attach a feed.

public static attachFeed(array& $element, string $href, string|null $title = null): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$href string
The feed href.
$title string|null
The feed title.

See Also:

  • \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processFeed

Attach a head link.

public static attachHeadLink(array& $element, string $href, string $rel, array $attributes = [], bool $shouldAddHeader = false): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$href string
The link href.
$rel string
The link rel.
$attributes array
Optionally more link attributes.
$shouldAddHeader bool
A boolean specifying whether the link should also be a Link: HTTP header.

See Also:

  • \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processHtmlHeadLink
  • https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link

attachStyle

Attach an inline CSS style.

public static attachStyle(array& $element, string $style, string $key = '#attributes'): void

The style is added after any existing inline style.

  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$style string
The inline CSS style value.
$key string
(optional) The element's attributes property to add the style to, such
as '#wrapper_attributes' for a form element's wrapper. Defaults to
'#attributes'.

mergeAccess

Change the access of a render element.

public static mergeAccess(array& $element, bool|\Drupal\Core\Access\AccessResultInterface $access, bool $and = true): void
  • This method is static. Parameters:
Parameter Type Description
$element array
The render element.
$access bool|\Drupal\Core\Access\AccessResultInterface
The access to merge.
$and bool
If the access should be an andIf if TRUE or orIf if FALSE.

findChildParents

Finds a child element by its key anywhere in a render element.

public static findChildParents(array $element, int|string $key): array|null

Searches the element's children, and their children, depth-first, so elements that were moved into a container, like a details element, are found too.

  • This method is static. Parameters:
Parameter Type Description
$element array
The render element, such as a form.
$key int|string
The key of the child element to find. Numeric keys and their string
form, like 0 and '0', match each other.

Return Value:

The keys from the element to the child, ending with the child's own key, for NestedArray::getValue() and NestedArray::setValue(), or NULL if no child has the key. When several do, the first one found is used.