Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Render / Element / Details.php
1 <?php
2
3 namespace Drupal\Core\Render\Element;
4
5 use Drupal\Core\Render\Element;
6
7 /**
8  * Provides a render element for a details element, similar to a fieldset.
9  *
10  * Fieldsets can only be used in forms, while details elements can be used
11  * outside of forms. Users click on the title to open or close the details
12  * element, showing or hiding the contained elements.
13  *
14  * Properties:
15  * - #title: The title of the details container. Defaults to "Details".
16  * - #open: Indicates whether the container should be open by default.
17  *   Defaults to FALSE.
18  * - #summary_attributes: An array of attributes to apply to the <summary>
19  *   element.
20  *
21  * Usage example:
22  * @code
23  * $form['author'] = array(
24  *   '#type' => 'details',
25  *   '#title' => $this->t('Author'),
26  * );
27  *
28  * $form['author']['name'] = array(
29  *   '#type' => 'textfield',
30  *   '#title' => $this->t('Name'),
31  * );
32  * @endcode
33  *
34  * @see \Drupal\Core\Render\Element\Fieldset
35  * @see \Drupal]Core\Render\Element\VerticalTabs
36  *
37  * @RenderElement("details")
38  */
39 class Details extends RenderElement {
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getInfo() {
45     $class = get_class($this);
46     return [
47       '#open' => FALSE,
48       '#summary_attributes' => [],
49       '#value' => NULL,
50       '#process' => [
51         [$class, 'processGroup'],
52         [$class, 'processAjaxForm'],
53       ],
54       '#pre_render' => [
55         [$class, 'preRenderDetails'],
56         [$class, 'preRenderGroup'],
57       ],
58       '#theme_wrappers' => ['details'],
59     ];
60   }
61
62   /**
63    * Adds form element theming to details.
64    *
65    * @param $element
66    *   An associative array containing the properties and children of the
67    *   details.
68    *
69    * @return
70    *   The modified element.
71    */
72   public static function preRenderDetails($element) {
73     Element::setAttributes($element, ['id']);
74
75     // The .js-form-wrapper class is required for #states to treat details like
76     // containers.
77     static::setAttributes($element, ['js-form-wrapper', 'form-wrapper']);
78
79     // Collapsible details.
80     $element['#attached']['library'][] = 'core/drupal.collapse';
81
82     // Open the detail if specified or if a child has an error.
83     if (!empty($element['#open']) || !empty($element['#children_errors'])) {
84       $element['#attributes']['open'] = 'open';
85     }
86
87     // Do not render optional details elements if there are no children.
88     if (isset($element['#parents'])) {
89       $group = implode('][', $element['#parents']);
90       if (!empty($element['#optional']) && !Element::getVisibleChildren($element['#groups'][$group])) {
91         $element['#printed'] = TRUE;
92       }
93     }
94
95     return $element;
96   }
97
98 }