3 namespace Drupal\Core\Render;
6 * The render context: a stack containing bubbleable rendering metadata.
8 * A stack of \Drupal\Core\Render\BubbleableMetadata objects.
10 * @see \Drupal\Core\Render\RendererInterface
11 * @see \Drupal\Core\Render\Renderer
12 * @see \Drupal\Core\Render\BubbleableMetadata
14 class RenderContext extends \SplStack {
17 * Updates the current frame of the stack.
19 * @param array &$element
20 * The element of the render array that has just been rendered. The stack
21 * frame for this element will be updated with the bubbleable rendering
22 * metadata of this element.
24 public function update(&$element) {
25 // The latest frame represents the bubbleable metadata for the subtree.
26 $frame = $this->pop();
27 // Update the frame, but also update the current element, to ensure it
28 // contains up-to-date information in case it gets render cached.
29 $updated_frame = BubbleableMetadata::createFromRenderArray($element)->merge($frame);
30 $updated_frame->applyTo($element);
31 $this->push($updated_frame);
37 * Whenever another level in the render array has been rendered, the stack
38 * must be bubbled, to merge its rendering metadata with that of the parent
41 public function bubble() {
42 // If there's only one frame on the stack, then this is the root call, and
43 // we can't bubble up further. ::renderRoot() will reset the stack, but we
44 // must not reset it here to allow users of ::executeInRenderContext() to
45 // access the stack directly.
46 if ($this->count() === 1) {
50 // Merge the current and the parent stack frame.
51 $current = $this->pop();
52 $parent = $this->pop();
53 $this->push($current->merge($parent));