function FieldUiTable::tablePreRender

Same name and namespace in other branches
  1. 9 core/modules/field_ui/src/Element/FieldUiTable.php \Drupal\field_ui\Element\FieldUiTable::tablePreRender()
  2. 8.9.x core/modules/field_ui/src/Element/FieldUiTable.php \Drupal\field_ui\Element\FieldUiTable::tablePreRender()
  3. 10 core/modules/field_ui/src/Element/FieldUiTable.php \Drupal\field_ui\Element\FieldUiTable::tablePreRender()

Performs pre-render tasks on field_ui_table elements.

Parameters

array $elements: A structured array containing two sub-levels of elements. Properties used:

  • #region_callback: A callback that provides the region of the table to place the row in.
  • #tabledrag: The value is a list of $options arrays that are passed to drupal_attach_tabledrag(). The HTML ID of the table is added to each $options array.

Return value

array The $element with prepared variables ready for field-ui-table.html.twig.

See also

\Drupal\Core\Render\RendererInterface::render()

\Drupal\Core\Render\Element\Table::preRenderTable()

File

core/modules/field_ui/src/Element/FieldUiTable.php, line 46

Class

FieldUiTable
Provides a field_ui table element.

Namespace

Drupal\field_ui\Element

Code

public static function tablePreRender($elements) {
    $js_settings = [];
    // For each region, build the tree structure from the weight and parenting
    // data contained in the flat form structure, to determine row order and
    // indentation.
    $regions = $elements['#regions'];
    $tree = [
        '' => [
            'name' => '',
            'children' => [],
        ],
    ];
    $trees = array_fill_keys(array_keys($regions), $tree);
    $parents = [];
    $children = Element::children($elements);
    $list = array_combine($children, $children);
    // Iterate on rows until we can build a known tree path for all of them.
    while ($list) {
        foreach ($list as $name) {
            $row =& $elements[$name];
            $parent = $row['parent_wrapper']['parent']['#value'];
            // Proceed if parent is known.
            if (empty($parent) || isset($parents[$parent])) {
                // Grab parent, and remove the row from the next iteration.
                $parents[$name] = $parent ? array_merge($parents[$parent], [
                    $parent,
                ]) : [];
                unset($list[$name]);
                // Determine the region for the row.
                $region_name = call_user_func_array($row['#region_callback'], [
                    &$row,
                ]);
                // Add the element in the tree.
                // phpcs:ignore DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable
                $target =& $trees[$region_name][''];
                foreach ($parents[$name] as $key) {
                    $target =& $target['children'][$key];
                }
                $target['children'][$name] = [
                    'name' => $name,
                    'weight' => $row['weight']['#value'],
                ];
                // Add tabledrag indentation to the first row cell.
                if ($depth = count($parents[$name])) {
                    $children = Element::children($row);
                    $cell = current($children);
                    $indentation = [
                        '#theme' => 'indentation',
                        '#size' => $depth,
                        '#suffix' => $row[$cell]['#prefix'] ?? '',
                    ];
                    $row[$cell]['#prefix'] = \Drupal::service('renderer')->render($indentation);
                }
                // Add row id and associate JS settings.
                $id = Html::getClass($name);
                $row['#attributes']['id'] = $id;
                if (isset($row['#js_settings'])) {
                    $row['#js_settings'] += [
                        'rowHandler' => $row['#row_type'],
                        'name' => $name,
                        'region' => $region_name,
                    ];
                    $js_settings[$id] = $row['#js_settings'];
                }
            }
        }
    }
    // Determine rendering order from the tree structure.
    foreach ($regions as $region_name => $region) {
        $elements['#regions'][$region_name]['rows_order'] = array_reduce($trees[$region_name], [
            static::class,
            'reduceOrder',
        ]);
    }
    $elements['#attached']['drupalSettings']['fieldUIRowsData'] = $js_settings;
    // If the custom #tabledrag is set and there is an HTML ID, add the table's
    // HTML ID to the options and attach the behavior.
    // @see \Drupal\Core\Render\Element\Table::preRenderTable()
    if (!empty($elements['#tabledrag']) && isset($elements['#attributes']['id'])) {
        foreach ($elements['#tabledrag'] as $options) {
            $options['table_id'] = $elements['#attributes']['id'];
            drupal_attach_tabledrag($elements, $options);
        }
    }
    return $elements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.