FALSE, static::FIELD_TYPE => '', static::FIELD_DATA => '', static::FIELD_EVENT_TYPE => static::EVENT_TYPE_NO_ERROR, ] + parent::defineDefaultProperties(); } /** * {@inheritdoc} */ protected function defineTranslatableProperties() { return array_merge( parent::defineTranslatableProperties(), ['null_label', static::FIELD_DATA], ); } /** * {@inheritdoc} */ public function supportsMultipleValues() { return FALSE; } /** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $form['element']['multiple']['#access'] = FALSE; $form[static::FIELD_TYPE] = [ '#type' => 'textfield', '#title' => $this->t('Type'), '#required' => TRUE, ]; $form[static::FIELD_DATA] = [ '#type' => 'textarea', '#title' => $this->t('JSON data'), '#required' => TRUE, '#element_validate' => [[$this, 'validateJson']], ]; $form[static::FIELD_EVENT_TYPE] = [ '#type' => 'checkboxes', '#title' => $this->t('Event type'), '#default_value' => '', '#required' => TRUE, '#options' => [ static::EVENT_TYPE_NO_ERROR => $this->t('On valid form only.'), static::EVENT_TYPE_ERROR => $this->t('On errors only.'), ], '#description' => $this->t('The element must be placed at the end of the form part to be efficient.') ]; return $form; } /** * Validate JSON. * * @param array $element * The element. * @param \Drupal\Core\Form\FormStateInterface $formState * The formstate. */ public function validateJson(array &$element, FormStateInterface $formState) { try { if (!Json::decode($element['#value'])) { $noJson = TRUE; } } catch (\Exception $e) { $noJson = TRUE; } if ($noJson) { $formState->setError($element, $this->t('The value is not json valid')); } } }