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