public static function WebformTableSort::processWebformTableSort in Webform 8.5
Same name and namespace in other branches
- 6.x src/Element/WebformTableSort.php \Drupal\webform\Element\WebformTableSort::processWebformTableSort()
Creates checkbox and weights to populate a 'webform_table_order' table.
Parameters
array $element: An associative array containing the properties and children of the webform_table_order element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
array $complete_form: The complete webform structure.
Return value
array The processed element.
File
- src/
Element/ WebformTableSort.php, line 150
Class
- WebformTableSort
- Provides a webform element for a sortable table element.
Namespace
Drupal\webform\ElementCode
public static function processWebformTableSort(&$element, FormStateInterface $form_state, &$complete_form) {
$value = is_array($element['#value']) ? $element['#value'] : [];
// Add validate callback that extracts the associative array of options.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformTableSelectOrder',
]);
$element['#tree'] = TRUE;
if (count($element['#options']) > 0) {
if (!isset($element['#default_value']) || $element['#default_value'] === 0) {
$element['#default_value'] = [];
}
// Place checked options first.
$options = [];
foreach ($value as $checked_option_key) {
if (isset($element['#options'][$checked_option_key])) {
$options[$checked_option_key] = $element['#options'][$checked_option_key];
unset($element['#options'][$checked_option_key]);
}
}
$options += $element['#options'];
$element['#options'] = $options;
// Set delta and default weight.
$delta = count($element['#options']);
$weight = 0;
foreach ($element['#options'] as $key => $choice) {
// Do not overwrite manually created children.
if (!isset($element[$key])) {
$weight_title = '';
if ($title = TableSelect::getTableSelectOptionTitle($choice)) {
$weight_title = new TranslatableMarkup('Weight for @title', [
'@title' => $title,
]);
}
$element[$key]['value'] = [
'#type' => 'hidden',
'#value' => $key,
];
$element[$key]['weight'] = [
'#type' => 'weight',
'#title' => $weight_title,
'#title_display' => 'invisible',
'#delta' => $delta,
'#default_value' => $weight++,
'#attributes' => [
'class' => [
'table-sort-weight',
],
],
];
}
}
}
else {
$element['#value'] = [];
}
// Enable tabledrag.
$element['#tabledrag'] = [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'table-sort-weight',
],
];
return $element;
}