class Text3Widget

Same name in other branches
  1. 3.x modules/field_example/src/Plugin/Field/FieldWidget/Text3Widget.php \Drupal\field_example\Plugin\Field\FieldWidget\Text3Widget
  2. 4.0.x modules/field_example/src/Plugin/Field/FieldWidget/Text3Widget.php \Drupal\field_example\Plugin\Field\FieldWidget\Text3Widget

Plugin implementation of the 'field_example_3text' widget.

Plugin annotation


@FieldWidget(
  id = "field_example_3text",
  module = "field_example",
  label = @Translation("RGB text field"),
  field_types = {
    "field_example_rgb"
  }
)

Hierarchy

  • class \Drupal\field_example\Plugin\Field\FieldWidget\Text3Widget extends \Drupal\Core\Field\WidgetBase

Expanded class hierarchy of Text3Widget

File

field_example/src/Plugin/Field/FieldWidget/Text3Widget.php, line 21

Namespace

Drupal\field_example\Plugin\Field\FieldWidget
View source
class Text3Widget extends WidgetBase {
    
    /**
     * {@inheritdoc}
     */
    public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
        $value = isset($items[$delta]->value) ? $items[$delta]->value : '';
        // Parse the single hex string into RBG values.
        if (!empty($value)) {
            preg_match_all('@..@', substr($value, 1), $match);
        }
        else {
            $match = [
                [],
            ];
        }
        // Set up the form element for this widget.
        $element += [
            '#type' => 'details',
            '#element_validate' => [
                [
                    $this,
                    'validate',
                ],
            ],
        ];
        // Add in the RGB textfield elements.
        foreach ([
            'r' => $this->t('Red'),
            'g' => $this->t('Green'),
            'b' => $this->t('Blue'),
        ] as $key => $title) {
            $element[$key] = [
                '#type' => 'textfield',
                '#title' => $title,
                '#size' => 2,
                '#default_value' => array_shift($match[0]),
                '#attributes' => [
                    'class' => [
                        'rgb-entry',
                    ],
                ],
                '#description' => $this->t('The 2-digit hexadecimal representation of @color saturation, like "a1" or "ff"', [
                    '@color' => $title,
                ]),
            ];
            // Since Form API doesn't allow a fieldset to be required, we
            // have to require each field element individually.
            if ($element['#required']) {
                $element[$key]['#required'] = TRUE;
            }
        }
        return [
            'value' => $element,
        ];
    }
    
    /**
     * Validate the fields and convert them into a single value as text.
     */
    public function validate($element, FormStateInterface $form_state) {
        // Validate each of the textfield entries.
        $values = [];
        foreach ([
            'r',
            'g',
            'b',
        ] as $colorfield) {
            $values[$colorfield] = $element[$colorfield]['#value'];
            // If they left any empty, we'll set the value empty and quit.
            if (strlen($values[$colorfield]) == 0) {
                $form_state->setValueForElement($element, '');
                return;
            }
            // If they gave us anything that's not hex, reject it.
            if (strlen($values[$colorfield]) != 2 || !ctype_xdigit($values[$colorfield])) {
                $form_state->setError($element[$colorfield], $form_state, $this->t("Saturation value must be a 2-digit hexadecimal value between 00 and ff."));
            }
        }
        // Set the value of the entire form element.
        $value = strtolower(sprintf('#%02s%02s%02s', $values['r'], $values['g'], $values['b']));
        $form_state->setValueForElement($element, $value);
    }

}

Members

Title Sort descending Modifiers Object type Summary
Text3Widget::formElement public function
Text3Widget::validate public function Validate the fields and convert them into a single value as text.