Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / lib / Drupal / Core / Render / PlaceholderGenerator.php
1 <?php
2
3 namespace Drupal\Core\Render;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Component\Utility\UrlHelper;
8 use Drupal\Core\Cache\Cache;
9
10 /**
11  * Turns a render array into a placeholder.
12  */
13 class PlaceholderGenerator implements PlaceholderGeneratorInterface {
14
15   /**
16    * The renderer configuration array.
17    *
18    * @var array
19    */
20   protected $rendererConfig;
21
22   /**
23    * Constructs a new Placeholder service.
24    *
25    * @param array $renderer_config
26    *   The renderer configuration array.
27    */
28   public function __construct(array $renderer_config) {
29     $this->rendererConfig = $renderer_config;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public function canCreatePlaceholder(array $element) {
36     return
37       // If generated by a #lazy_builder callback, placeholdering is possible.
38       isset($element['#lazy_builder'])
39       &&
40       // If #create_placeholder === FALSE, placeholdering is disallowed.
41       (!isset($element['#create_placeholder']) || $element['#create_placeholder'] !== FALSE);
42   }
43
44   /**
45    * {@inheritdoc}
46    */
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
50     // parameter.
51     $conditions = $this->rendererConfig['auto_placeholder_conditions'];
52
53     if (isset($element['#cache']['max-age']) && $element['#cache']['max-age'] !== Cache::PERMANENT && $element['#cache']['max-age'] <= $conditions['max-age']) {
54       return TRUE;
55     }
56
57     if (isset($element['#cache']['contexts']) && array_intersect($element['#cache']['contexts'], $conditions['contexts'])) {
58       return TRUE;
59     }
60
61     if (isset($element['#cache']['tags']) && array_intersect($element['#cache']['tags'], $conditions['tags'])) {
62       return TRUE;
63     }
64
65     return FALSE;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
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.
79       '#cache' => TRUE,
80     ]);
81
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<<<
85     // debugging.
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>';
90
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;
96   }
97
98 }