WebformTriggerApi.php 3.47 KB
Newer Older
Thomas SECHER's avatar
Thomas SECHER committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php

namespace Drupal\trigger_api\Plugin\WebformElement;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElementBase;

/**
 * Provides a 'webform_example_element' element.
 *
 * @WebformElement(
 *   id = "webform_trigger_api",
 *   label = @Translation("Trigger API"),
 *   description = @Translation("Provides a webform element for trigger api."),
 *   category = @Translation("Trigger"),
 * )
 *
 * @see \Drupal\webform_example_element\Element\WebformExampleElement
 * @see \Drupal\webform\Plugin\WebformElementBase
 * @see \Drupal\webform\Plugin\WebformElementInterface
 * @see \Drupal\webform\Annotation\WebformElement
 */
class WebformTriggerApi extends WebformElementBase {

  /**
   * FIELD Type.
   *
   * @const string
   */
  public const FIELD_TYPE = 'trigger_type';

  /**
   * Field data.
   *
   * @const string
   */
  public const FIELD_DATA = 'trigger_data';

  /**
   * Field trigger event.
   *
   * @const string
   */
  public const FIELD_EVENT_TYPE = 'trigger_event_type';

  /**
   * Event Type error.
   *
   * @const string
   */
  public const EVENT_TYPE_ERROR = 'trigger_event_type_error';

  /**
   * Event type no error.
   *
   * @const string
   */
  public const EVENT_TYPE_NO_ERROR = 'trigger_event_type_no_error';

  /**
   * {@inheritdoc}
   */
  protected function defineDefaultProperties() {
    return [
Thomas SECHER's avatar
Thomas SECHER committed
66
67
68
        'multiple'               => FALSE,
        static::FIELD_TYPE       => '',
        static::FIELD_DATA       => '',
Thomas SECHER's avatar
Thomas SECHER committed
69
70
71
72
73
74
75
76
77
78
79
        static::FIELD_EVENT_TYPE => static::EVENT_TYPE_NO_ERROR,

      ] + parent::defineDefaultProperties();
  }

  /**
   * {@inheritdoc}
   */
  protected function defineTranslatableProperties() {
    return array_merge(
      parent::defineTranslatableProperties(),
Thomas SECHER's avatar
Thomas SECHER committed
80
      ['null_label', static::FIELD_DATA],
Thomas SECHER's avatar
Thomas SECHER committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    );
  }

  /**
   * {@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,
Thomas SECHER's avatar
Thomas SECHER committed
117
      '#options'       => [
Thomas SECHER's avatar
Thomas SECHER committed
118
        static::EVENT_TYPE_NO_ERROR => $this->t('On valid form only.'),
Thomas SECHER's avatar
Thomas SECHER committed
119
        static::EVENT_TYPE_ERROR    => $this->t('On errors only.'),
Thomas SECHER's avatar
Thomas SECHER committed
120
      ],
Thomas SECHER's avatar
Thomas SECHER committed
121
      '#description'   => $this->t('The element must be placed at the end of the form part to be efficient.')
Thomas SECHER's avatar
Thomas SECHER committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    ];

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

}