summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2014-05-30 17:36:06 (GMT)
committerNathaniel Catchpole2014-05-30 17:36:06 (GMT)
commit49be3541fee1657c445d96032e37ffbb756e4352 (patch)
tree5dc8fc80e70aabe8948890d3d464f5cf6b792a6e
parent03117bc360072f1c4e2ed66e7de334c8a50a3cc0 (diff)
Issue #2275679 by Dave Reid, Wim Leers: Clean up render cache placeholder.
-rw-r--r--core/includes/common.inc34
-rw-r--r--core/modules/comment/src/CommentViewBuilder.php6
-rw-r--r--core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php6
-rw-r--r--core/modules/node/src/NodeViewBuilder.php7
-rw-r--r--core/modules/system/src/Tests/Common/RenderTest.php16
-rw-r--r--core/modules/system/tests/modules/common_test/common_test.module2
6 files changed, 30 insertions, 41 deletions
diff --git a/core/includes/common.inc b/core/includes/common.inc
index 252baca..5572e54 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -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));
}
}
diff --git a/core/modules/comment/src/CommentViewBuilder.php b/core/modules/comment/src/CommentViewBuilder.php
index a2521ac..e993e7a 100644
--- a/core/modules/comment/src/CommentViewBuilder.php
+++ b/core/modules/comment/src/CommentViewBuilder.php
@@ -120,15 +120,15 @@ class CommentViewBuilder extends EntityViewBuilder {
'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 @@ class CommentViewBuilder extends EntityViewBuilder {
*/
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'),
diff --git a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
index 19dd00f..290a607 100644
--- a/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
+++ b/core/modules/comment/src/Plugin/Field/FieldFormatter/CommentDefaultFormatter.php
@@ -181,15 +181,15 @@ class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryP
'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 @@ class CommentDefaultFormatter extends FormatterBase implements ContainerFactoryP
*/
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
diff --git a/core/modules/node/src/NodeViewBuilder.php b/core/modules/node/src/NodeViewBuilder.php
index 494183f..606ef36 100644
--- a/core/modules/node/src/NodeViewBuilder.php
+++ b/core/modules/node/src/NodeViewBuilder.php
@@ -40,16 +40,15 @@ class NodeViewBuilder extends EntityViewBuilder {
'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 @@ class NodeViewBuilder extends EntityViewBuilder {
*/
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',
diff --git a/core/modules/system/src/Tests/Common/RenderTest.php b/core/modules/system/src/Tests/Common/RenderTest.php
index caa80a2..8edbb07 100644
--- a/core/modules/system/src/Tests/Common/RenderTest.php
+++ b/core/modules/system/src/Tests/Common/RenderTest.php
@@ -771,16 +771,16 @@ class RenderTest extends DrupalUnitTestBase {
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 @@ class RenderTest extends DrupalUnitTestBase {
$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 @@ class RenderTest extends DrupalUnitTestBase {
);
$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 @@ class RenderTest extends DrupalUnitTestBase {
$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 @@ class RenderTest extends DrupalUnitTestBase {
$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 @@ class RenderTest extends DrupalUnitTestBase {
$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,
diff --git a/core/modules/system/tests/modules/common_test/common_test.module b/core/modules/system/tests/modules/common_test/common_test.module
index 2785be9..31ad453 100644
--- a/core/modules/system/tests/modules/common_test/common_test.module
+++ b/core/modules/system/tests/modules/common_test/common_test.module
@@ -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(