Same name and namespace in other branches
- 8.x-2.x src/Plugin/Field/FieldWidget/ColorFieldWidgetBox.php \Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldWidgetBox::settingsColorValidate()
Use element validator to make sure that hex values are in correct format.
Paramètres
mixed[] $element: The Default colors element.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Fichier
-
src/
Plugin/ Field/ FieldWidget/ ColorFieldWidgetBox.php, line 51
Classe
- ColorFieldWidgetBox
- Plugin implementation of the color_field box widget.
Namespace
Drupal\color_field\Plugin\Field\FieldWidgetCode
public function settingsColorValidate(array $element, FormStateInterface $form_state) : void {
$default_colors = $form_state->getValue($element['#parents']);
$colors = '';
if (!empty($default_colors)) {
preg_match_all("/#[0-9a-f]{6}/i", $default_colors, $default_colors, PREG_SET_ORDER);
foreach ($default_colors as $color) {
if (!empty($colors)) {
$colors .= ',';
}
$colors .= strtolower($color[0]);
}
}
$form_state->setValue($element['#parents'], $colors);
}