diff --git a/core/lib/Drupal/Core/Render/Element/Actions.php b/core/lib/Drupal/Core/Render/Element/Actions.php index 2e75165..bb490f5 100644 --- a/core/lib/Drupal/Core/Render/Element/Actions.php +++ b/core/lib/Drupal/Core/Render/Element/Actions.php @@ -32,12 +32,16 @@ class Actions extends Container { public function getInfo() { $class = get_class($this); return [ + '#optional' => FALSE, '#process' => [ // @todo Move this to #pre_render. [$class, 'preRenderActionsDropbutton'], [$class, 'processActions'], [$class, 'processContainer'], ], + '#pre_render' => [ + [$class, 'preRenderContainer'], + ] '#weight' => 100, '#theme_wrappers' => ['container'], ]; diff --git a/core/lib/Drupal/Core/Render/Element/Container.php b/core/lib/Drupal/Core/Render/Element/Container.php index c7b79f7..3538d3f 100644 --- a/core/lib/Drupal/Core/Render/Element/Container.php +++ b/core/lib/Drupal/Core/Render/Element/Container.php @@ -12,6 +12,10 @@ * Surrounds child elements with a
and adds attributes such as classes or * an HTML ID. * + * Properties: + * - #optional: Indicates whether the container should not render when it has + * no visible children. Defaults to FALSE. + * * Usage example: * @code * $form['needs_accommodation'] = array( @@ -47,6 +51,7 @@ class Container extends RenderElement { public function getInfo() { $class = get_class($this); return [ + '#optional' => FALSE, '#process' => [ [$class, 'processGroup'], [$class, 'processContainer'], @@ -88,12 +93,12 @@ public static function processContainer(&$element, FormStateInterface $form_stat * An associative array containing the properties and children of the * container. * - * @return + * @return array * The modified element. */ public static function preRenderContainer($element) { // Do not render optional container elements if there are no children. - if (!empty($element['#optional']) && !Element::getVisibleChildren($element)) { + if (!empty($element['#printed']) && !empty($element['#optional']) && !Element::getVisibleChildren($element)) { $element['#printed'] = TRUE; } return $element; diff --git a/core/modules/system/tests/src/Functional/Form/ElementsContainerTest.php b/core/modules/system/tests/src/Functional/Form/ElementsContainerTest.php index cb87569..a7d404b 100644 --- a/core/modules/system/tests/src/Functional/Form/ElementsContainerTest.php +++ b/core/modules/system/tests/src/Functional/Form/ElementsContainerTest.php @@ -23,10 +23,11 @@ class ElementsContainerTest extends BrowserTestBase { */ public function testOptionalContainerElements() { $this->drupalGet('form-test/optional-container'); - $this->assertSession()->elementNotExists('css', 'div.empty_optional'); - $this->assertSession()->elementExists('css', 'div.empty_nonoptional'); - $this->assertSession()->elementExists('css', 'div.nonempty_optional'); - $this->assertSession()->elementExists('css', 'div.nonempty_nonoptional'); + $assertSession = $this->assertSession(); + $assertSession->elementNotExists('css', 'div.empty_optional'); + $assertSession->elementExists('css', 'div.empty_nonoptional'); + $assertSession->elementExists('css', 'div.nonempty_optional'); + $assertSession->elementExists('css', 'div.nonempty_nonoptional'); } }