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