diff --git a/core/includes/common.inc b/core/includes/common.inc index 2def62d..d11a3f8 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -3992,7 +3992,20 @@ function render(&$element) { } if (is_array($element)) { show($element); - return drupal_render($element, TRUE); + + // Try to get the rendered element from the static cache. This only + // persists for the current request, and is needed because we test if a + // render array produces output by rendering it and checking if the output + // is empty. So without this, if the element is not cached by drupal_render, + // it is rendered for each test, and also for printing on page. + $static_cache = &drupal_static(__FUNCTION__); + $static_cid = md5(serialize($element)); + + if (!isset($static_cache[$static_cid])) { + $static_cache[$static_cid] = drupal_render($element, TRUE); + } + + return $static_cache[$static_cid]; } else { // Safe-guard for inappropriate use of render() on flat variables: return @@ -4057,6 +4070,20 @@ function show(&$element) { } /** + * Tests if a render array produces output in the current context. + * + * @param array $element + * The render array to test. + * + * @return boolean + * Whether the render array produced content or not. + */ +function is_printable($element) { + $rendered = render($element); + return !empty($rendered); +} + +/** * Gets the cached, prerendered element of a renderable element from the cache. * * @param array $elements diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php index 4cb31ca..d16745a 100644 --- a/core/lib/Drupal/Core/Template/TwigExtension.php +++ b/core/lib/Drupal/Core/Template/TwigExtension.php @@ -56,6 +56,15 @@ public function getFilters() { /** * {@inheritdoc} */ + public function getTests() { + return array( + new \Twig_SimpleTest('printable', 'is_printable'), + ); + } + + /** + * {@inheritdoc} + */ public function getNodeVisitors() { // The node visitor is needed to wrap all variables with // render_var -> twig_render_var() function. diff --git a/core/modules/aggregator/templates/aggregator-item.html.twig b/core/modules/aggregator/templates/aggregator-item.html.twig index 8c23106..76ec14c 100644 --- a/core/modules/aggregator/templates/aggregator-item.html.twig +++ b/core/modules/aggregator/templates/aggregator-item.html.twig @@ -28,7 +28,7 @@ {{ source_date }} - {% if content %} + {% if content is printable %}
{{ parent }}
{% endif %} @@ -97,7 +97,7 @@{{ snippet }}
{% endif %} - {% if info %} + {% if info is printable %}{{ info }}
{% endif %}