3 namespace Drupal\field_ui\Element;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Render\Element;
7 use Drupal\Core\Render\Element\Table;
10 * Provides a field_ui table element.
12 * @RenderElement("field_ui_table")
14 class FieldUiTable extends Table {
19 public function getInfo() {
20 $info = parent::getInfo();
21 $info['#regions'] = ['' => []];
22 $info['#theme'] = 'field_ui_table';
23 // Prepend FieldUiTable's prerender callbacks.
24 array_unshift($info['#pre_render'], [$this, 'tablePreRender'], [$this, 'preRenderRegionRows']);
29 * Performs pre-render tasks on field_ui_table elements.
31 * @param array $elements
32 * A structured array containing two sub-levels of elements. Properties
34 * - #tabledrag: The value is a list of $options arrays that are passed to
35 * drupal_attach_tabledrag(). The HTML ID of the table is added to each
39 * The $element with prepared variables ready for field-ui-table.html.twig.
41 * @see drupal_render()
42 * @see \Drupal\Core\Render\Element\Table::preRenderTable()
44 public static function tablePreRender($elements) {
47 // For each region, build the tree structure from the weight and parenting
48 // data contained in the flat form structure, to determine row order and
50 $regions = $elements['#regions'];
51 $tree = ['' => ['name' => '', 'children' => []]];
52 $trees = array_fill_keys(array_keys($regions), $tree);
55 $children = Element::children($elements);
56 $list = array_combine($children, $children);
58 // Iterate on rows until we can build a known tree path for all of them.
60 foreach ($list as $name) {
61 $row = &$elements[$name];
62 $parent = $row['parent_wrapper']['parent']['#value'];
63 // Proceed if parent is known.
64 if (empty($parent) || isset($parents[$parent])) {
65 // Grab parent, and remove the row from the next iteration.
66 $parents[$name] = $parent ? array_merge($parents[$parent], [$parent]) : [];
69 // Determine the region for the row.
70 $region_name = call_user_func_array($row['#region_callback'], [&$row]);
72 // Add the element in the tree.
73 $target = &$trees[$region_name][''];
74 foreach ($parents[$name] as $key) {
75 $target = &$target['children'][$key];
77 $target['children'][$name] = ['name' => $name, 'weight' => $row['weight']['#value']];
79 // Add tabledrag indentation to the first row cell.
80 if ($depth = count($parents[$name])) {
81 $children = Element::children($row);
82 $cell = current($children);
84 '#theme' => 'indentation',
86 '#suffix' => isset($row[$cell]['#prefix']) ? $row[$cell]['#prefix'] : '',
88 $row[$cell]['#prefix'] = \Drupal::service('renderer')->render($indentation);
91 // Add row id and associate JS settings.
92 $id = Html::getClass($name);
93 $row['#attributes']['id'] = $id;
94 if (isset($row['#js_settings'])) {
95 $row['#js_settings'] += [
96 'rowHandler' => $row['#row_type'],
98 'region' => $region_name,
100 $js_settings[$id] = $row['#js_settings'];
106 // Determine rendering order from the tree structure.
107 foreach ($regions as $region_name => $region) {
108 $elements['#regions'][$region_name]['rows_order'] = array_reduce($trees[$region_name], [static::class, 'reduceOrder']);
111 $elements['#attached']['drupalSettings']['fieldUIRowsData'] = $js_settings;
113 // If the custom #tabledrag is set and there is a HTML ID, add the table's
114 // HTML ID to the options and attach the behavior.
115 // @see \Drupal\Core\Render\Element\Table::preRenderTable()
116 if (!empty($elements['#tabledrag']) && isset($elements['#attributes']['id'])) {
117 foreach ($elements['#tabledrag'] as $options) {
118 $options['table_id'] = $elements['#attributes']['id'];
119 drupal_attach_tabledrag($elements, $options);
127 * Performs pre-render to move #regions to rows.
129 * @param array $elements
130 * A structured array containing two sub-levels of elements. Properties
132 * - #tabledrag: The value is a list of $options arrays that are passed to
133 * drupal_attach_tabledrag(). The HTML ID of the table is added to each
137 * The $element with prepared variables ready for field-ui-table.html.twig.
139 public static function preRenderRegionRows($elements) {
140 // Determine the colspan to use for region rows, by checking the number of
141 // columns in the headers.
143 foreach ($elements['#header'] as $header) {
144 $columns_count += (is_array($header) && isset($header['colspan']) ? $header['colspan'] : 1);
148 foreach (Element::children($elements) as $key) {
149 $rows[$key] = $elements[$key];
150 unset($elements[$key]);
153 // Render rows, region by region.
154 foreach ($elements['#regions'] as $region_name => $region) {
155 $region_name_class = Html::getClass($region_name);
158 if (isset($region['title']) && empty($region['invisible'])) {
159 $elements['#rows'][] = [
162 'region-' . $region_name_class . '-title'
164 'no_striping' => TRUE,
166 ['data' => $region['title'], 'colspan' => $columns_count],
170 if (isset($region['message'])) {
171 $class = (empty($region['rows_order']) ? 'region-empty' : 'region-populated');
172 $elements['#rows'][] = [
175 'region-' . $region_name_class . '-message', $class,
177 'no_striping' => TRUE,
179 ['data' => $region['message'], 'colspan' => $columns_count],
184 // Add form rows, in the order determined at pre-render time.
185 foreach ($region['rows_order'] as $name) {
186 $element = $rows[$name];
188 $row = ['data' => []];
189 if (isset($element['#attributes'])) {
190 $row += $element['#attributes'];
193 // Render children as table cells.
194 foreach (Element::children($element) as $cell_key) {
195 $child = $element[$cell_key];
196 // Do not render a cell for children of #type 'value'.
197 if (!(isset($child['#type']) && $child['#type'] == 'value')) {
198 $cell = ['data' => $child];
199 if (isset($child['#cell_attributes'])) {
200 $cell += $child['#cell_attributes'];
202 $row['data'][] = $cell;
205 $elements['#rows'][] = $row;
213 * Determines the rendering order of an array representing a tree.
215 * Callback for array_reduce() within ::tablePreRender().
217 * @param mixed $array
218 * Holds the return value of the previous iteration; in the case of the
219 * first iteration it instead holds the value of the initial array.
221 * Holds the value of the current iteration.
224 * Array where rendering order has been determined.
226 public static function reduceOrder($array, $a) {
227 $array = !$array ? [] : $array;
229 $array[] = $a['name'];
231 if (!empty($a['children'])) {
232 uasort($a['children'], ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
233 $array = array_merge($array, array_reduce($a['children'], [static::class, 'reduceOrder']));