Open Data Catalog v2.0.0
JsonSchemaFormWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8 
22 class JsonSchemaFormWidget extends WidgetBase {
23 
29  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
30  $value = isset($items[$delta]->value) ? $items[$delta]->value : '';
31  ksm($value);
32  ksm('y');
33  $element += [
34  '#type' => 'textfield',
35  '#default_value' => $value,
36  '#size' => 7,
37  '#maxlength' => 7,
38  '#element_validate' => [
39  [$this, 'validate'],
40  ],
41  ];
42  return ['value' => $element];
43  }
44 
48  public function validate($element, FormStateInterface $form_state) {
49  $value = $element['#value'];
50  ksm('h');
51  ksm($value);
52  if (strlen($value) == 0) {
53  $form_state->setValueForElement($element, '');
54  return;
55  }
56  if (!preg_match('/^#([a-f0-9]{6})$/iD', strtolower($value))) {
57  $form_state->setError($element, $this->t("Color must be a 6-digit hexadecimal value, suitable for CSS."));
58  }
59  }
60 
61 }
Drupal\json_schema_field\Plugin\Field\FieldWidget
Definition: JsonSchemaFormWidget.php:3
Drupal\json_schema_field\Plugin\Field\FieldWidget\JsonSchemaFormWidget
Definition: JsonSchemaFormWidget.php:22
Drupal\json_schema_field\Plugin\Field\FieldWidget\JsonSchemaFormWidget\formElement
formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
Definition: JsonSchemaFormWidget.php:29
Drupal\json_schema_field\Plugin\Field\FieldWidget\JsonSchemaFormWidget\validate
validate($element, FormStateInterface $form_state)
Definition: JsonSchemaFormWidget.php:48