function Tableselect::preRenderTableselect
Same name in other branches
- 9 core/lib/Drupal/Core/Render/Element/Tableselect.php \Drupal\Core\Render\Element\Tableselect::preRenderTableselect()
- 10 core/lib/Drupal/Core/Render/Element/Tableselect.php \Drupal\Core\Render\Element\Tableselect::preRenderTableselect()
- 11.x core/lib/Drupal/Core/Render/Element/Tableselect.php \Drupal\Core\Render\Element\Tableselect::preRenderTableselect()
Prepares a 'tableselect' #type element for rendering.
Adds a column of radio buttons or checkboxes for each row of a table.
Parameters
array $element: An associative array containing the properties and children of the tableselect element. Properties used: #header, #options, #empty, and #js_select. The #options property is an array of selection options; each array element of #options is an array of properties. These properties can include #attributes, which is added to the table row's HTML attributes; see table.html.twig. An example of per-row options:
$options = array(
array(
'title' => $this->t('How to Learn Drupal'),
'content_type' => $this->t('Article'),
'status' => 'published',
'#attributes' => array(
'class' => array(
'article-row',
),
),
),
array(
'title' => $this->t('Privacy Policy'),
'content_type' => $this->t('Page'),
'status' => 'published',
'#attributes' => array(
'class' => array(
'page-row',
),
),
),
);
$header = array(
'title' => $this->t('Title'),
'content_type' => $this->t('Content type'),
'status' => $this->t('Status'),
);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => $this->t('No content available.'),
);
Return value
array The processed element.
File
-
core/
lib/ Drupal/ Core/ Render/ Element/ Tableselect.php, line 149
Class
- Tableselect
- Provides a form element for a table with radios or checkboxes in left column.
Namespace
Drupal\Core\Render\ElementCode
public static function preRenderTableselect($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'] = [];
if (isset($element['#options'][$key]['#attributes'])) {
$row += $element['#options'][$key]['#attributes'];
}
// Render the checkbox / radio element.
$row['data'][] = \Drupal::service('renderer')->render($element[$key]);
// 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];
}
}
}
$rows[] = $row;
}
// Add an empty header or a "Select all" checkbox to provide room for the
// checkboxes/radios in the first table column.
if ($element['#js_select']) {
// Add a "Select all" checkbox.
$element['#attached']['library'][] = 'core/drupal.tableselect';
array_unshift($header, [
'class' => [
'select-all',
],
]);
}
else {
// Add an empty header when radio buttons are displayed or a "Select all"
// checkbox is not desired.
array_unshift($header, '');
}
}
$element['#header'] = $header;
$element['#rows'] = $rows;
return $element;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.