public function InlineParagraphsWidget::massageFormValues in Paragraphs 8
Massages the form values into the format expected for field values.
Parameters
array $values: The submitted form values produced by the widget.
- If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
- If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.
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.
Return value
array An array of field values, keyed by delta.
Overrides WidgetBase::massageFormValues
File
- src/
Plugin/ Field/ FieldWidget/ InlineParagraphsWidget.php, line 1303
Class
- InlineParagraphsWidget
- Plugin implementation of the 'entity_reference paragraphs' widget.
Namespace
Drupal\paragraphs\Plugin\Field\FieldWidgetCode
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
$field_name = $this->fieldDefinition
->getName();
$widget_state = static::getWidgetState($form['#parents'], $field_name, $form_state);
$element = NestedArray::getValue($form_state
->getCompleteForm(), $widget_state['array_parents']);
foreach ($values as &$item) {
if (isset($widget_state['paragraphs'][$item['_original_delta']]['entity']) && $widget_state['paragraphs'][$item['_original_delta']]['mode'] != 'remove') {
$paragraphs_entity = $widget_state['paragraphs'][$item['_original_delta']]['entity'];
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
$display = $widget_state['paragraphs'][$item['_original_delta']]['display'];
if ($widget_state['paragraphs'][$item['_original_delta']]['mode'] == 'edit') {
$display
->extractFormValues($paragraphs_entity, $element[$item['_original_delta']]['subform'], $form_state);
}
// A content entity form saves without any rebuild. It needs to set the
// language to update it in case of language change.
$langcode_key = $paragraphs_entity
->getEntityType()
->getKey('langcode');
if ($paragraphs_entity
->get($langcode_key)->value != $form_state
->get('langcode')) {
// If a translation in the given language already exists, switch to
// that. If there is none yet, update the language.
if ($paragraphs_entity
->hasTranslation($form_state
->get('langcode'))) {
$paragraphs_entity = $paragraphs_entity
->getTranslation($form_state
->get('langcode'));
}
else {
$paragraphs_entity
->set($langcode_key, $form_state
->get('langcode'));
}
}
$paragraphs_entity
->setNeedsSave(TRUE);
$item['entity'] = $paragraphs_entity;
$item['target_id'] = $paragraphs_entity
->id();
$item['target_revision_id'] = $paragraphs_entity
->getRevisionId();
}
elseif (isset($widget_state['paragraphs'][$item['_original_delta']]['mode']) && in_array($widget_state['paragraphs'][$item['_original_delta']]['mode'], [
'remove',
'removed',
])) {
$item['target_id'] = NULL;
$item['target_revision_id'] = NULL;
}
}
return $values;
}