3 namespace Drupal\Core\Render\Element;
5 use Drupal\Component\Utility\Html as HtmlUtility;
6 use Drupal\Core\Form\FormStateInterface;
9 * Provides a render element that wraps child elements in a container.
11 * Surrounds child elements with a <div> and adds attributes such as classes or
16 * $form['needs_accommodation'] = array(
17 * '#type' => 'checkbox',
18 * '#title' => $this->t('Need Special Accommodations?'),
21 * $form['accommodation'] = array(
22 * '#type' => 'container',
23 * '#attributes' => array(
24 * 'class' => 'accommodation',
27 * 'invisible' => array(
28 * 'input[name="needs_accommodation"]' => array('checked' => FALSE),
33 * $form['accommodation']['diet'] = array(
34 * '#type' => 'textfield',
35 * '#title' => $this->t('Dietary Restrictions'),
39 * @RenderElement("container")
41 class Container extends RenderElement {
46 public function getInfo() {
47 $class = get_class($this);
50 [$class, 'processGroup'],
51 [$class, 'processContainer'],
54 [$class, 'preRenderGroup'],
56 '#theme_wrappers' => ['container'],
61 * Processes a container element.
63 * @param array $element
64 * An associative array containing the properties and children of the
66 * @param \Drupal\Core\Form\FormStateInterface $form_state
67 * The current state of the form.
68 * @param array $complete_form
69 * The complete form structure.
72 * The processed element.
74 public static function processContainer(&$element, FormStateInterface $form_state, &$complete_form) {
75 // Generate the ID of the element if it's not explicitly given.
76 if (!isset($element['#id'])) {
77 $element['#id'] = HtmlUtility::getUniqueId(implode('-', $element['#parents']) . '-wrapper');