PlaceholderGenerator.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Drupal\Core\Render;
  3. use Drupal\Component\Utility\Crypt;
  4. use Drupal\Component\Utility\Html;
  5. use Drupal\Component\Utility\UrlHelper;
  6. use Drupal\Core\Cache\Cache;
  7. /**
  8. * Turns a render array into a placeholder.
  9. */
  10. class PlaceholderGenerator implements PlaceholderGeneratorInterface {
  11. /**
  12. * The renderer configuration array.
  13. *
  14. * @var array
  15. */
  16. protected $rendererConfig;
  17. /**
  18. * Constructs a new Placeholder service.
  19. *
  20. * @param array $renderer_config
  21. * The renderer configuration array.
  22. */
  23. public function __construct(array $renderer_config) {
  24. $this->rendererConfig = $renderer_config;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function canCreatePlaceholder(array $element) {
  30. // If generated by a #lazy_builder callback, placeholdering is possible.
  31. return isset($element['#lazy_builder']) &&
  32. // If #create_placeholder === FALSE, placeholdering is disallowed.
  33. (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function shouldAutomaticallyPlaceholder(array $element) {
  39. // Auto-placeholder if the max-age, cache context or cache tag is specified
  40. // in the auto-placeholder conditions in the 'renderer.config' container
  41. // parameter.
  42. $conditions = $this->rendererConfig['auto_placeholder_conditions'];
  43. if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) {
  44. return TRUE;
  45. }
  46. if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) {
  47. return TRUE;
  48. }
  49. if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) {
  50. return TRUE;
  51. }
  52. return FALSE;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function createPlaceholder(array $element) {
  58. $placeholder_render_array = array_intersect_key($element, [
  59. // Placeholders are replaced with markup by executing the associated
  60. // #lazy_builder callback, which generates a render array, and which the
  61. // Renderer will render and replace the placeholder with.
  62. '#lazy_builder' => TRUE,
  63. // The cacheability metadata for the placeholder. The rendered result of
  64. // the placeholder may itself be cached, if [#cache][keys] are specified.
  65. '#cache' => TRUE,
  66. ]);
  67. // Generate placeholder markup. Note that the only requirement is that this
  68. // is unique markup that isn't easily guessable. The #lazy_builder callback
  69. // and its arguments are put in the placeholder markup solely to simplify<<<
  70. // debugging.
  71. $callback = $placeholder_render_array['#lazy_builder'][0];
  72. $arguments = UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]);
  73. $token = Crypt::hashBase64(serialize($placeholder_render_array));
  74. $placeholder_markup = '<drupal-render-placeholder callback="' . Html::escape($callback) . '" arguments="' . Html::escape($arguments) . '" token="' . Html::escape($token) . '"></drupal-render-placeholder>';
  75. // Build the placeholder element to return.
  76. $placeholder_element = [];
  77. $placeholder_element['#markup'] = Markup::create($placeholder_markup);
  78. $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array;
  79. return $placeholder_element;
  80. }
  81. }