3 namespace Drupal\Core\Render;
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Component\Utility\UrlHelper;
8 use Drupal\Core\Cache\Cache;
11 * Turns a render array into a placeholder.
13 class PlaceholderGenerator implements PlaceholderGeneratorInterface {
16 * The renderer configuration array.
20 protected $rendererConfig;
23 * Constructs a new Placeholder service.
25 * @param array $renderer_config
26 * The renderer configuration array.
28 public function __construct(array $renderer_config) {
29 $this->rendererConfig = $renderer_config;
35 public function canCreatePlaceholder(array $element) {
37 // If generated by a #lazy_builder callback, placeholdering is possible.
38 isset($element['#lazy_builder'])
40 // If #create_placeholder === FALSE, placeholdering is disallowed.
41 (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE);
47 public function shouldAutomaticallyPlaceholder(array $element) {
48 // Auto-placeholder if the max-age, cache context or cache tag is specified
49 // in the auto-placeholder conditions in the 'renderer.config' container
51 $conditions = $this->rendererConfig['auto_placeholder_conditions'];
53 if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) {
57 if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) {
61 if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) {
71 public function createPlaceholder(array $element) {
72 $placeholder_render_array = array_intersect_key($element, [
73 // Placeholders are replaced with markup by executing the associated
74 // #lazy_builder callback, which generates a render array, and which the
75 // Renderer will render and replace the placeholder with.
76 '#lazy_builder' => TRUE,
77 // The cacheability metadata for the placeholder. The rendered result of
78 // the placeholder may itself be cached, if [#cache][keys] are specified.
82 // Generate placeholder markup. Note that the only requirement is that this
83 // is unique markup that isn't easily guessable. The #lazy_builder callback
84 // and its arguments are put in the placeholder markup solely to simplify<<<
86 $callback = $placeholder_render_array['#lazy_builder'][0];
87 $arguments = UrlHelper::buildQuery($placeholder_render_array['#lazy_builder'][1]);
88 $token = Crypt::hashBase64(serialize($placeholder_render_array));
89 $placeholder_markup = '<drupal-render-placeholder callback="' . Html::escape($callback) . '" arguments="' . Html::escape($arguments) . '" token="' . Html::escape($token) . '"></drupal-render-placeholder>';
91 // Build the placeholder element to return.
92 $placeholder_element = [];
93 $placeholder_element['#markup'] = Markup::create($placeholder_markup);
94 $placeholder_element['#attached']['placeholders'][$placeholder_markup] = $placeholder_render_array;
95 return $placeholder_element;