public static function MediaForm::removeItem in GridStack 8.2
Submit callback for remove buttons.
File
- src/
Plugin/ gridstack/ stylizer/ MediaForm.php, line 550
Class
- MediaForm
- Provides the media form for Layout Builder integration.
Namespace
Drupal\gridstack\Plugin\gridstack\stylizerCode
public static function removeItem(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state
->getTriggeringElement();
// Get the parents required to find the top-level widget element.
if (count($triggering_element['#array_parents']) < 4) {
throw new \LogicException('Expected the remove button to be more than four levels deep in the form. Triggering element parents were: ' . implode(',', $triggering_element['#array_parents']));
}
$parents = array_slice($triggering_element['#array_parents'], 0, -3);
$element = NestedArray::getValue($form, $parents);
// Get the field state.
$path = $element['#parents'];
$values = NestedArray::getValue($form_state
->getValues(), $path);
$field_state = static::getFieldState($element, $form_state);
// Get the delta of the item being removed.
$delta = array_slice($triggering_element['#array_parents'], -2, 1)[0];
if (isset($values['selection'][$delta])) {
// Add the weight of the removed item to the field state so we can shift
// focus to the next/previous item in an easy way.
$field_state['removed_item_weight'] = $values['selection'][$delta]['weight'];
$field_state['removed_item_id'] = $triggering_element['#media_id'];
unset($values['selection'][$delta]);
$field_state['items'] = $values['selection'];
static::setFieldState($element, $form_state, $field_state);
}
$form_state
->setRebuild();
}