function ListItemBase::validateAllowedValues
Same name in other branches
- 9 core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php \Drupal\options\Plugin\Field\FieldType\ListItemBase::validateAllowedValues()
- 8.9.x core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php \Drupal\options\Plugin\Field\FieldType\ListItemBase::validateAllowedValues()
- 10 core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php \Drupal\options\Plugin\Field\FieldType\ListItemBase::validateAllowedValues()
#element_validate callback for options field allowed values.
Parameters
$element: An associative array containing the properties and children of the generic form element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form for the form this element belongs to.
See also
\Drupal\Core\Render\Element\FormElementBase::processPattern()
File
-
core/
modules/ options/ src/ Plugin/ Field/ FieldType/ ListItemBase.php, line 347
Class
- ListItemBase
- Plugin base class inherited by the options field types.
Namespace
Drupal\options\Plugin\Field\FieldTypeCode
public static function validateAllowedValues($element, FormStateInterface $form_state) {
$items = array_filter(array_map(function ($item) use ($element) {
$current_element = $element['table'][$item];
if ($current_element['item']['key']['#value'] !== NULL && $current_element['item']['label']['#value']) {
return $current_element['item']['key']['#value'] . '|' . $current_element['item']['label']['#value'];
}
elseif ($current_element['item']['key']['#value']) {
return $current_element['item']['key']['#value'];
}
elseif ($current_element['item']['label']['#value']) {
return $current_element['item']['label']['#value'];
}
return NULL;
}, Element::children($element['table'])), function ($item) {
return $item;
});
if ($reordered_items = $form_state->getValue([
$element['#parents'],
'table',
])) {
uksort($items, function ($a, $b) use ($reordered_items) {
$a_weight = $reordered_items[$a]['weight'] ?? 0;
$b_weight = $reordered_items[$b]['weight'] ?? 0;
return $a_weight <=> $b_weight;
});
}
$values = static::extractAllowedValues($items, $element['#field_has_data']);
if (!is_array($values)) {
$form_state->setError($element, new TranslatableMarkup('Allowed values list: invalid input.'));
}
else {
// Check that keys are valid for the field type.
foreach ($values as $key => $value) {
if ($error = static::validateAllowedValue($key)) {
$form_state->setError($element, $error);
break;
}
}
$form_state->setValueForElement($element, $values);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.