Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Manuel Garcia on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.5.x
Introduced in version:
8.5.0-alpha1
Issue links:
Description:
The Container render element now supports the #optional
property. It allows control on whether or not to render the element when it has no visible children.
Before:
Independent of whether or not the container had any visible elements, the markup for the container would be produced, resulting in empty div elements:
$form['my-empty-container'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => 'my-class',
),
'child' => [],
);
Will produce this empty div: <div class="my-class"></div>
After:
The new #optional
property defaults to FALSE
, so if you do not specify this property, the same empty div will be produced. However, if you set #optional
to TRUE:
$form['my-empty-container'] = array(
'#type' => 'container',
'#optional' => TRUE,
'#attributes' => array(
'class' => 'my-class',
),
'child' => [],
);
No markup will be produced.
Impacts:
Module developers
Themers