core/includes/common.inc | 20 +++++++++++- .../tests/modules/common_test/common_test.module | 34 ++++++++------------ 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/core/includes/common.inc b/core/includes/common.inc index 2e28710..b553d6c 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -4275,11 +4275,29 @@ function _drupal_render_process_post_render_cache(array &$elements) { $modified_elements = $elements; foreach ($elements['#post_render_cache'] as $callback => $options) { foreach ($elements['#post_render_cache'][$callback] as $token => $context) { + // The advanced option, when setting #post_render_cache directly. if (is_numeric($token)) { $modified_elements = call_user_func_array($callback, array($modified_elements, $context)); } + // The simple option, when using the standard placeholders, and hence + // also when using #type => render_cache_placeholder. else { - $modified_elements = call_user_func_array($callback, array($modified_elements, $context, $token)); + // Call #post_render_cache callback to generate the element that will + // fill in the placeholder. + $generated_element = call_user_func_array($callback, array($context)); + + // Update #attached based on the generated element. + if (isset($generated_element['#attached'])) { + if (!isset($modified_elements['#attached'])) { + $modified_elements['#attached'] = array(); + } + $modified_elements['#attached'] = drupal_merge_attached($modified_elements['#attached'], drupal_render_collect_attached($generated_element, TRUE)); + } + + // Replace the placeholder with the rendered markup of the generated + // element. + $placeholder = drupal_render_cache_generate_placeholder($callback, $context, $token); + $modified_elements['#markup'] = str_replace($placeholder, drupal_render($generated_element), $modified_elements['#markup']); } } } 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 cae051c..b3b8ad8 100644 --- a/core/modules/system/tests/modules/common_test/common_test.module +++ b/core/modules/system/tests/modules/common_test/common_test.module @@ -233,31 +233,25 @@ function common_test_post_render_cache(array $element, array $context) { /** * #post_render_cache callback; replaces placeholder, extends #attached. * - * @param array $element - * A render array with the following keys: - * - #markup - * - #attached * @param array $context * An array with the following keys: * - bar: contains a random string. - * @param string $token - * A unique token to uniquely identify the placeholder. * - * @return array $element - * The updated $element. + * @return array + * A render array. */ -function common_test_post_render_cache_placeholder(array $element, array $context, $token) { - // Generate exact placeholder string. - $placeholder = drupal_render_cache_generate_placeholder(__FUNCTION__, $context, $token); - - // Replace placeholder in markup. - $element['#markup'] = str_replace($placeholder, '' . $context['bar'] . '', $element['#markup']); - - // Extend #attached. - $element['#attached']['js'][] = array( - 'type' => 'setting', - 'data' => array( - 'common_test' => $context +function common_test_post_render_cache_placeholder(array $context) { + $element = array( + '#markup' => '' . $context['bar'] . '', + '#attached' => array( + 'js' => array( + array( + 'type' => 'setting', + 'data' => array( + 'common_test' => $context, + ), + ), + ), ), );