public function OgComplex::massageFormValues in Organic groups 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 EntityReferenceAutocompleteWidget::massageFormValues
File
- src/
Plugin/ Field/ FieldWidget/ OgComplex.php, line 324
Class
- OgComplex
- Plugin implementation of the 'entity_reference autocomplete' widget.
Namespace
Drupal\og\Plugin\Field\FieldWidgetCode
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
// Remove empty values. The form fields may be empty.
$values = array_filter($values, function ($item) {
return !empty($item['target_id']);
});
// Get the groups from the other groups widget.
foreach ($form[$this->fieldDefinition
->getName()]['other_groups'] as $key => $value) {
if (!is_int($key)) {
continue;
}
// Matches the entity label and ID. E.g. 'Label (123)'. The entity ID will
// be captured in it's own group, with the key 'id'.
preg_match("|.+\\((?<id>[\\w.]+)\\)|", $value['target_id']['#value'], $matches);
if (!empty($matches['id'])) {
$values[] = [
'target_id' => $matches['id'],
'_weight' => $value['_weight']['#value'],
'_original_delta' => $value['_weight']['#delta'],
];
}
}
return $values;
}