public static function EntitySubqueueForm::submitAction in Entityqueue 8
Submit callback for the 'reverse', 'shuffle' and 'clear' actions.
File
- src/
Form/ EntitySubqueueForm.php, line 175
Class
- EntitySubqueueForm
- Form controller for the entity subqueue edit forms.
Namespace
Drupal\entityqueue\FormCode
public static function submitAction(array &$form, FormStateInterface $form_state) {
$trigger = $form_state
->getTriggeringElement();
$op = $trigger['#op'];
// Check if we have a form element for the 'items' field.
$path = array_merge($form['#parents'], [
'items',
]);
$key_exists = NULL;
NestedArray::getValue($form_state
->getValues(), $path, $key_exists);
if ($key_exists) {
// Remove any user input for the 'items' element in order to allow the
// values set below to be applied.
$user_input = $form_state
->getUserInput();
NestedArray::setValue($user_input, $path, NULL);
$form_state
->setUserInput($user_input);
$entity = $form_state
->getFormObject()
->getEntity();
$items_widget = $form_state
->getFormObject()
->getFormDisplay($form_state)
->getRenderer('items');
$subqueue_items = $entity
->get('items');
$items_widget
->extractFormValues($subqueue_items, $form, $form_state);
$items_values = $subqueue_items
->getValue();
switch ($op) {
case 'reverse':
$subqueue_items
->setValue(array_reverse($items_values));
break;
case 'shuffle':
shuffle($items_values);
$subqueue_items
->setValue($items_values);
break;
case 'clear':
// Set the items count to zero.
$parents = NestedArray::getValue($form, $path)['widget']['#field_parents'];
$field_state = WidgetBase::getWidgetState($parents, 'items', $form_state);
$field_state['items_count'] = 0;
WidgetBase::setWidgetState($parents, 'items', $form_state, $field_state);
$subqueue_items
->setValue(NULL);
break;
}
// Handle 'inline_entity_form' widgets separately because they have a
// custom form state storage for the current state of the referenced
// entities.
if (\Drupal::moduleHandler()
->moduleExists('inline_entity_form') && $items_widget instanceof InlineEntityFormBase) {
$items_form_element = NestedArray::getValue($form, $path);
$ief_id = $items_form_element['widget']['#ief_id'];
$entities = $form_state
->get([
'inline_entity_form',
$ief_id,
'entities',
]);
if (isset($entities)) {
$form_state
->set([
'inline_entity_form',
$ief_id,
'entities',
], []);
switch ($op) {
case 'reverse':
$entities = array_reverse($entities);
break;
case 'shuffle':
shuffle($entities);
break;
case 'clear':
$entities = [];
break;
}
foreach ($entities as $delta => $item) {
$item['weight'] = $delta;
$form_state
->set([
'inline_entity_form',
$ief_id,
'entities',
$delta,
], $item);
}
}
}
// Handle 'entity_browser' widgets separately because they have a custom
// form state storage for the current state of the referenced entities.
if (\Drupal::moduleHandler()
->moduleExists('entity_browser') && $items_widget instanceof EntityReferenceBrowserWidget) {
$ids = array_column($subqueue_items
->getValue(), 'target_id');
$widget_id = $subqueue_items
->getEntity()
->uuid() . ':' . $subqueue_items
->getFieldDefinition()
->getName();
$form_state
->set([
'entity_browser_widget',
$widget_id,
], $ids);
}
$form_state
->getFormObject()
->setEntity($entity);
$form_state
->setRebuild();
}
}