Same name and namespace in other branches
  1. 8.x-2.x src/Plugin/Field/FieldWidget/ColorFieldWidgetBox.php \Drupal\color_field\Plugin\Field\FieldWidget\ColorFieldWidgetBox::settingsColorValidate() 1 comment

Use element validator to make sure that hex values are in correct format.

Parameters

mixed[] $element: The Default colors element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

src/Plugin/Field/FieldWidget/ColorFieldWidgetBox.php, line 51

Class

ColorFieldWidgetBox
Plugin implementation of the color_field box widget.

Namespace

Drupal\color_field\Plugin\Field\FieldWidget

Code

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);
}