public function InlineEntityFormSimple::extractFormValues in Inline Entity Form 8
Extracts field values from submitted form values.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values. This parameter is altered by reference to receive the incoming form values.
array $form: The form structure where field elements are attached to. This might be a full form structure, or a sub-element of a larger form.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Overrides WidgetBase::extractFormValues
File
- src/
Plugin/ Field/ FieldWidget/ InlineEntityFormSimple.php, line 126
Class
- InlineEntityFormSimple
- Simple inline widget.
Namespace
Drupal\inline_entity_form\Plugin\Field\FieldWidgetCode
public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) {
if ($this
->isDefaultValueWidget($form_state)) {
$items
->filterEmptyItems();
return;
}
$field_name = $this->fieldDefinition
->getName();
$parents = array_merge($form['#parents'], [
$field_name,
]);
$submitted_values = $form_state
->getValue($parents);
$values = [];
foreach ($items as $delta => $value) {
$element = NestedArray::getValue($form, [
$field_name,
'widget',
$delta,
]);
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $element['inline_entity_form']['#entity'];
$weight = isset($submitted_values[$delta]['_weight']) ? $submitted_values[$delta]['_weight'] : 0;
$values[$weight] = [
'entity' => $entity,
];
}
// Sort items base on weights.
ksort($values);
$values = array_values($values);
// Let the widget massage the submitted values.
$values = $this
->massageFormValues($values, $form, $form_state);
// Assign the values and remove the empty ones.
$items
->setValue($values);
$items
->filterEmptyItems();
// Populate the IEF form state with $items so that WidgetSubmit can
// perform the necessary saves.
$ief_id = $this
->makeIefId($parents);
$widget_state = [
'instance' => $this->fieldDefinition,
'delete' => [],
'entities' => [],
];
foreach ($items as $delta => $value) {
TranslationHelper::updateEntityLangcode($value->entity, $form_state);
$widget_state['entities'][$delta] = [
'entity' => $value->entity,
'needs_save' => TRUE,
];
}
$form_state
->set([
'inline_entity_form',
$ief_id,
], $widget_state);
// Put delta mapping in $form_state, so that flagErrors() can use it.
$field_name = $this->fieldDefinition
->getName();
$field_state = WidgetBase::getWidgetState($form['#parents'], $field_name, $form_state);
foreach ($items as $delta => $item) {
$field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta;
unset($item->_original_delta, $item->weight);
}
WidgetBase::setWidgetState($form['#parents'], $field_name, $form_state, $field_state);
}