summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/Render
diff options
context:
space:
mode:
authorLauri Eskola2017-08-16 04:36:47 (GMT)
committerLauri Eskola2017-08-16 04:36:47 (GMT)
commitcff5bad8fbf5f5238b590525a765ba092992ec16 (patch)
tree5f32d915ff149f4b6c80dcb36da56681cb081bfc /core/lib/Drupal/Core/Render
parent4aa7190baa4ba02508f11d67af85aaed4f4fed2e (diff)
Issue #2893586 by Manuel Garcia, amateescu, vijaycs85, dawehner: Add #optional support to the Container render element
Diffstat (limited to 'core/lib/Drupal/Core/Render')
-rw-r--r--core/lib/Drupal/Core/Render/Element/Container.php25
1 files changed, 25 insertions, 0 deletions
diff --git a/core/lib/Drupal/Core/Render/Element/Container.php b/core/lib/Drupal/Core/Render/Element/Container.php
index 212000d..357e6a5 100644
--- a/core/lib/Drupal/Core/Render/Element/Container.php
+++ b/core/lib/Drupal/Core/Render/Element/Container.php
@@ -4,6 +4,7 @@ namespace Drupal\Core\Render\Element;
use Drupal\Component\Utility\Html as HtmlUtility;
use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Render\Element;
/**
* Provides a render element that wraps child elements in a container.
@@ -11,6 +12,10 @@ use Drupal\Core\Form\FormStateInterface;
* Surrounds child elements with a <div> and adds attributes such as classes or
* an HTML ID.
*
+ * Properties:
+ * - #optional: Indicates whether the container should render when it has no
+ * visible children. Defaults to FALSE.
+ *
* Usage example:
* @code
* $form['needs_accommodation'] = array(
@@ -46,12 +51,14 @@ class Container extends RenderElement {
public function getInfo() {
$class = get_class($this);
return [
+ '#optional' => FALSE,
'#process' => [
[$class, 'processGroup'],
[$class, 'processContainer'],
],
'#pre_render' => [
[$class, 'preRenderGroup'],
+ [$class, 'preRenderContainer'],
],
'#theme_wrappers' => ['container'],
];
@@ -79,4 +86,22 @@ class Container extends RenderElement {
return $element;
}
+ /**
+ * Prevents optional containers from rendering if they have no children.
+ *
+ * @param array $element
+ * An associative array containing the properties and children of the
+ * container.
+ *
+ * @return array
+ * The modified element.
+ */
+ public static function preRenderContainer($element) {
+ // Do not render optional container elements if there are no children.
+ if (empty($element['#printed']) && !empty($element['#optional']) && !Element::getVisibleChildren($element)) {
+ $element['#printed'] = TRUE;
+ }
+ return $element;
+ }
+
}