Commit 49be3541 authored by catch's avatar catch

Issue #2275679 by Dave Reid, Wim Leers: Clean up render cache placeholder.

parent 03117bc3
......@@ -3666,29 +3666,22 @@ function drupal_render_cache_set(&$markup, array $elements) {
* The #post_render_cache callback that will replace the placeholder with its
* eventual markup.
* @param array $context
* An array providing context for the #post_render_cache callback.
* @param string $token
* A unique token to uniquely identify the placeholder.
* An array providing context for the #post_render_cache callback. This array
* will be altered to provide a 'token' key/value pair, if not already
* provided, to uniquely identify the generated placeholder.
*
* @return string
* The generated placeholder HTML.
*
* @see drupal_render_cache_get()
*/
function drupal_render_cache_generate_placeholder($callback, array $context, $token) {
// Serialize the context into a HTML attribute; unserializing is unnecessary.
$context_attribute = '';
foreach ($context as $key => $value) {
$context_attribute .= $key . ':' . $value . ';';
}
return '<drupal:render-cache-placeholder callback="' . $callback . '" context="' . $context_attribute . '" token="' . $token . '" />';
}
function drupal_render_cache_generate_placeholder($callback, array &$context) {
// Generate a unique token if one is not already provided.
$context += array(
'token' => \Drupal\Component\Utility\Crypt::randomBytesBase64(55),
);
/**
* Generates a unique token for use in a #post_render_cache placeholder.
*/
function drupal_render_cache_generate_token() {
return \Drupal\Component\Utility\Crypt::randomBytesBase64(55);
return '<drupal:render-cache-placeholder callback="' . $callback . '" token="' . $context['token'] . '" />';
}
/**
......@@ -3710,12 +3703,9 @@ function drupal_render_cache_generate_token() {
*/
function _drupal_render_process_post_render_cache(array &$elements) {
if (isset($elements['#post_render_cache'])) {
// Call all #post_render_cache callbacks, while passing the provided context
// and if keyed by a number, no token is passed, otherwise, the token string
// is passed to the callback as well. This token is used to uniquely
// identify the placeholder in the markup.
foreach ($elements['#post_render_cache'] as $callback => $options) {
foreach ($elements['#post_render_cache'][$callback] as $token => $context) {
// Call all #post_render_cache callbacks, passing the provided context.
foreach (array_keys($elements['#post_render_cache']) as $callback) {
foreach ($elements['#post_render_cache'][$callback] as $context) {
$elements = call_user_func_array($callback, array($elements, $context));
}
}
......
......@@ -120,15 +120,15 @@ public function buildComponents(array &$build, array $entities, array $displays,
'commented_entity_type' => $commented_entity->getEntityTypeId(),
'commented_entity_id' => $commented_entity->id(),
'in_preview' => !empty($entity->in_preview),
'token' => drupal_render_cache_generate_token(),
);
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$build[$id]['links'] = array(
'#post_render_cache' => array(
$callback => array(
$context,
),
),
'#markup' => drupal_render_cache_generate_placeholder($callback, $context, $context['token']),
'#markup' => $placeholder,
);
if (!isset($build[$id]['#attached'])) {
......@@ -167,7 +167,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
*/
public static function renderLinks(array $element, array $context) {
$callback = '\Drupal\comment\CommentViewBuilder::renderLinks';
$placeholder = drupal_render_cache_generate_placeholder($callback, $context, $context['token']);
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$links = array(
'#theme' => 'links__comment',
'#pre_render' => array('drupal_pre_render_links'),
......
......@@ -181,15 +181,15 @@ public function viewElements(FieldItemListInterface $items) {
'entity_type' => $entity->getEntityTypeId(),
'entity_id' => $entity->id(),
'field_name' => $field_name,
'token' => drupal_render_cache_generate_token(),
);
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$output['comment_form'] = array(
'#post_render_cache' => array(
$callback => array(
$context,
),
),
'#markup' => drupal_render_cache_generate_placeholder($callback, $context, $context['token']),
'#markup' => $placeholder,
);
}
}
......@@ -223,7 +223,7 @@ public function viewElements(FieldItemListInterface $items) {
*/
public static function renderForm(array $element, array $context) {
$callback = '\Drupal\comment\Plugin\Field\FieldFormatter\CommentDefaultFormatter::renderForm';
$placeholder = drupal_render_cache_generate_placeholder($callback, $context, $context['token']);
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$entity = entity_load($context['entity_type'], $context['entity_id']);
$form = comment_add($entity, $context['field_name']);
// @todo: This only works as long as assets are still tracked in a global
......
......@@ -40,16 +40,15 @@ public function buildComponents(array &$build, array $entities, array $displays,
'view_mode' => $view_mode,
'langcode' => $langcode,
'in_preview' => !empty($entity->in_preview),
'token' => drupal_render_cache_generate_token(),
);
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$build[$id]['links'] = array(
'#post_render_cache' => array(
$callback => array(
$context,
),
),
'#markup' => drupal_render_cache_generate_placeholder($callback, $context, $context['token']),
'#markup' => $placeholder,
);
......@@ -105,7 +104,7 @@ protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langco
*/
public static function renderLinks(array $element, array $context) {
$callback = '\Drupal\node\NodeViewBuilder::renderLinks';
$placeholder = drupal_render_cache_generate_placeholder($callback, $context, $context['token']);
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$links = array(
'#theme' => 'links__node',
......
......@@ -771,16 +771,16 @@ function testDrupalRenderChildrenPostRenderCache() {
function testDrupalRenderRenderCachePlaceholder() {
$context = array(
'bar' => $this->randomContextValue(),
'token' => drupal_render_cache_generate_token(),
);
$callback = 'common_test_post_render_cache_placeholder';
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$test_element = array(
'#post_render_cache' => array(
$callback => array(
$context
),
),
'#markup' => drupal_render_cache_generate_placeholder($callback, $context, $context['token']),
'#markup' => $placeholder,
'#prefix' => '<foo>',
'#suffix' => '</foo>'
);
......@@ -825,7 +825,7 @@ function testDrupalRenderRenderCachePlaceholder() {
$this->assertIdentical($token, $expected_token, 'The tokens are identical');
// Verify the token is in the cached element.
$expected_element = array(
'#markup' => '<foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" context="bar:' . $context['bar'] . ';token:' . $expected_token . ';" token="'. $expected_token . '" /></foo>',
'#markup' => '<foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" token="'. $expected_token . '" /></foo>',
'#post_render_cache' => array(
'common_test_post_render_cache_placeholder' => array(
$context
......@@ -860,16 +860,16 @@ function testDrupalRenderChildElementRenderCachePlaceholder() {
);
$context = array(
'bar' => $this->randomContextValue(),
'token' => drupal_render_cache_generate_token(),
);
$callback = 'common_test_post_render_cache_placeholder';
$placeholder = drupal_render_cache_generate_placeholder($callback, $context);
$test_element = array(
'#post_render_cache' => array(
$callback => array(
$context
),
),
'#markup' => drupal_render_cache_generate_placeholder($callback, $context, $context['token']),
'#markup' => $placeholder,
'#prefix' => '<foo>',
'#suffix' => '</foo>'
);
......@@ -923,7 +923,7 @@ function testDrupalRenderChildElementRenderCachePlaceholder() {
$this->assertIdentical($token, $expected_token, 'The tokens are identical for the child element');
// Verify the token is in the cached element.
$expected_element = array(
'#markup' => '<foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" context="bar:' . $context['bar'] . ';token:' . $expected_token . ';" token="'. $expected_token . '" /></foo>',
'#markup' => '<foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" token="'. $expected_token . '" /></foo>',
'#post_render_cache' => array(
'common_test_post_render_cache_placeholder' => array(
$context,
......@@ -948,7 +948,7 @@ function testDrupalRenderChildElementRenderCachePlaceholder() {
$this->assertIdentical($token, $expected_token, 'The tokens are identical for the parent element');
// Verify the token is in the cached element.
$expected_element = array(
'#markup' => '<div><foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" context="bar:' . $context['bar'] . ';token:' . $expected_token . ';" token="'. $expected_token . '" /></foo></div>' . "\n",
'#markup' => '<div><foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" token="'. $expected_token . '" /></foo></div>' . "\n",
'#post_render_cache' => array(
'common_test_post_render_cache_placeholder' => array(
$context,
......@@ -977,7 +977,7 @@ function testDrupalRenderChildElementRenderCachePlaceholder() {
$this->assertIdentical($token, $expected_token, 'The tokens are identical for the child element');
// Verify the token is in the cached element.
$expected_element = array(
'#markup' => '<foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" context="bar:' . $context['bar'] . ';token:' . $expected_token . ';" token="'. $expected_token . '" /></foo>',
'#markup' => '<foo><drupal:render-cache-placeholder callback="common_test_post_render_cache_placeholder" token="'. $expected_token . '" /></foo>',
'#post_render_cache' => array(
'common_test_post_render_cache_placeholder' => array(
$context,
......
......@@ -207,7 +207,7 @@ function common_test_post_render_cache(array $element, array $context) {
* A render array.
*/
function common_test_post_render_cache_placeholder(array $element, array $context) {
$placeholder = drupal_render_cache_generate_placeholder(__FUNCTION__, $context, $context['token']);
$placeholder = drupal_render_cache_generate_placeholder(__FUNCTION__, $context);
$replace_element = array(
'#markup' => '<bar>' . $context['bar'] . '</bar>',
'#attached' => array(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment