public static function WebformTableSort::preRenderWebformTableSort in Webform 8.5
Same name and namespace in other branches
- 6.x src/Element/WebformTableSort.php \Drupal\webform\Element\WebformTableSort::preRenderWebformTableSort()
Prepares a 'webform_table_sort' #type element for rendering.
Return value
array The processed element.
File
- src/
Element/ WebformTableSort.php, line 78
Class
- WebformTableSort
- Provides a webform element for a sortable table element.
Namespace
Drupal\webform\ElementCode
public static function preRenderWebformTableSort($element) {
$rows = [];
$header = $element['#header'];
if (!empty($element['#options'])) {
// Generate a table row for each selectable item in #options.
foreach (Element::children($element) as $key) {
$row = [];
$row['data'] = [];
// Set the row to be draggable.
$row['class'] = [
'draggable',
];
if (isset($element['#options'][$key]['#attributes'])) {
$row += $element['#options'][$key]['#attributes'];
}
// As table.html.twig only maps header and row columns by order, create
// the correct order by iterating over the header fields.
foreach ($element['#header'] as $fieldname => $title) {
// A row cell can span over multiple headers, which means less row
// cells than headers could be present.
if (isset($element['#options'][$key][$fieldname])) {
// A header can span over multiple cells and in this case the cells
// are passed in an array. The order of this array determines the
// order in which they are added.
if (is_array($element['#options'][$key][$fieldname]) && !isset($element['#options'][$key][$fieldname]['data'])) {
foreach ($element['#options'][$key][$fieldname] as $cell) {
$row['data'][] = $cell;
}
}
else {
$row['data'][] = $element['#options'][$key][$fieldname];
}
}
}
// Render the weight and hidden value element.
$weight = [
$element[$key]['weight'],
$element[$key]['value'],
];
$row['data'][] = \Drupal::service('renderer')
->render($weight);
$rows[] = $row;
}
}
// Append weight to header.
$header[] = t('Weight');
// Set header and rows.
$element['#header'] = $header;
$element['#rows'] = $rows;
// Attach table sort.
$element['#attributes']['class'][] = 'js-table-sort';
$element['#attributes']['class'][] = 'table-sort';
return $element;
}