RenderContext.php 1.8 KB

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