function template_preprocess_file_widget_multiple
Same name in other branches
- 8.9.x core/modules/file/file.field.inc \template_preprocess_file_widget_multiple()
- 10 core/modules/file/file.module \template_preprocess_file_widget_multiple()
- 11.x core/modules/file/file.module \template_preprocess_file_widget_multiple()
Prepares variables for multi file form widget templates.
Default template: file-widget-multiple.html.twig.
Parameters
array $variables: An associative array containing:
- element: A render element representing the widgets.
File
-
core/
modules/ file/ file.field.inc, line 21
Code
function template_preprocess_file_widget_multiple(&$variables) {
$element = $variables['element'];
// Special ID and classes for draggable tables.
$weight_class = $element['#id'] . '-weight';
$table_id = $element['#id'] . '-table';
// Build up a table of applicable fields.
$headers = [];
$headers[] = t('File information');
if ($element['#display_field']) {
$headers[] = [
'data' => t('Display'),
'class' => [
'checkbox',
],
];
}
$headers[] = t('Weight');
$headers[] = t('Operations');
// Get our list of widgets in order (needed when the form comes back after
// preview or failed validation).
$widgets = [];
foreach (Element::children($element) as $key) {
$widgets[] =& $element[$key];
}
usort($widgets, '_field_multiple_value_form_sort_helper');
$rows = [];
foreach ($widgets as $key => &$widget) {
// Save the uploading row for last.
if (empty($widget['#files'])) {
$widget['#title'] = $element['#file_upload_title'];
$widget['#description'] = \Drupal::service('renderer')->renderPlain($element['#file_upload_description']);
continue;
}
// Delay rendering of the buttons, so that they can be rendered later in the
// "operations" column.
$operations_elements = [];
foreach (Element::children($widget) as $sub_key) {
if (isset($widget[$sub_key]['#type']) && $widget[$sub_key]['#type'] == 'submit') {
hide($widget[$sub_key]);
$operations_elements[] =& $widget[$sub_key];
}
}
// Delay rendering of the "Display" option and the weight selector, so that
// each can be rendered later in its own column.
if ($element['#display_field']) {
hide($widget['display']);
}
hide($widget['_weight']);
$widget['_weight']['#attributes']['class'] = [
$weight_class,
];
// Render everything else together in a column, without the normal wrappers.
$row = [];
$widget['#theme_wrappers'] = [];
$row[] = \Drupal::service('renderer')->render($widget);
// Arrange the row with the rest of the rendered columns.
if ($element['#display_field']) {
unset($widget['display']['#title']);
$row[] = [
'data' => $widget['display'],
'class' => [
'checkbox',
],
];
}
$row[] = [
'data' => $widget['_weight'],
];
// Show the buttons that had previously been marked as hidden in this
// preprocess function. We use show() to undo the earlier hide().
foreach (Element::children($operations_elements) as $key) {
show($operations_elements[$key]);
}
$row[] = [
'data' => $operations_elements,
];
$rows[] = [
'data' => $row,
'class' => isset($widget['#attributes']['class']) ? array_merge($widget['#attributes']['class'], [
'draggable',
]) : [
'draggable',
],
];
}
$variables['table'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#attributes' => [
'id' => $table_id,
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => $weight_class,
],
],
'#access' => !empty($rows),
];
$variables['element'] = $element;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.