public static function Tablefield::ajaxCallbackRebuild in TableField 8.2
AJAX callback to rebuild the number of rows/columns.
The basic idea is to descend down the list of #parent elements of the triggering_element in order to locate the tablefield inside of the $form array.
That is the element that we need to return.
Parameters
array $form: Form array.
\Drupal\Core\Form\FormStateInterface $form_state: Form state object.
File
- src/
Element/ Tablefield.php, line 241
Class
- Tablefield
- Provides a form element for tabular data.
Namespace
Drupal\tablefield\ElementCode
public static function ajaxCallbackRebuild(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
// Go as deep as 'tablefield' key, but stop there (two more keys follow).
$parents = array_slice($triggering_element['#array_parents'], 0, -2, TRUE);
$rebuild = NestedArray::getValue($form, $parents);
// We don't want to re-send the format/_weight options.
unset($rebuild['format']);
unset($rebuild['_weight']);
// Set row value to default only if there is Add Row button clicked.
$op = (string) $triggering_element['#value'];
if ($op === 'Add Row') {
$rebuild['rebuild']['rows']['#value'] = $rebuild['rebuild']['rows']['#default_value'];
}
return $rebuild;
}