public static function ViewfieldWidgetSelect::validateElement in Viewfield 8.3
Overridden form validation handler for widget elements.
Save selected target_id as a single item, since there will be at most one. This prevents the value from being deeply nested inside $form_state.
Parameters
array $element: The form element.
\Drupal\Core\Form\FormStateInterface $form_state: The form state.
Overrides OptionsWidgetBase::validateElement
See also
OptionsWidgetBase::validateElement()
File
- src/
Plugin/ Field/ FieldWidget/ ViewfieldWidgetSelect.php, line 217
Class
- ViewfieldWidgetSelect
- Plugin implementation of the 'viewfield_select' widget.
Namespace
Drupal\viewfield\Plugin\Field\FieldWidgetCode
public static function validateElement(array $element, FormStateInterface $form_state) {
if ($element['#required'] && $element['#value'] == '_none') {
$form_state
->setError($element, t('@name field is required.', [
'@name' => $element['#title'],
]));
}
// Massage submitted form values.
// Drupal\Core\Field\WidgetBase::submit() expects values as
// an array of values keyed by delta first, then by column, while our
// widgets return the opposite.
if (is_array($element['#value'])) {
$values = array_values($element['#value']);
}
else {
$values = [
$element['#value'],
];
}
// Filter out the 'none' option. Use a strict comparison, because
// 0 == 'any string'.
$index = array_search('_none', $values, TRUE);
if ($index !== FALSE) {
unset($values[$index]);
}
$target_id = !empty($values[0]) ? $values[0] : NULL;
$form_state
->setValueForElement($element, $target_id);
}