Completed
Push — 8.x-1.x ( 3d8643...b71662 )
by Janez
03:27
created

EntityBrowserForm::buildForm()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 44
rs 8.8571
c 3
b 0
f 0
cc 3
eloc 30
nc 4
nop 2
1
<?php
2
3
namespace Drupal\entity_browser\Form;
4
5
use Drupal\Component\Uuid\UuidInterface;
6
use Drupal\Core\Form\FormBase;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\entity_browser\DisplayAjaxInterface;
9
use Drupal\entity_browser\EntityBrowserFormInterface;
10
use Drupal\entity_browser\EntityBrowserInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * The entity browser form.
15
 */
16
class EntityBrowserForm extends FormBase implements EntityBrowserFormInterface {
17
18
  /**
19
   * UUID generator service.
20
   *
21
   * @var \Drupal\Component\Uuid\UuidInterface
22
   */
23
  protected $uuidGenerator;
24
25
  /**
26
   * The entity browser object.
27
   *
28
   * @var \Drupal\entity_browser\EntityBrowserInterface
29
   */
30
  protected $entity_browser;
31
32
  /**
33
   * Constructs a EntityBrowserForm object.
34
   *
35
   * @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
36
   *   The UUID generator service.
37
   */
38
  public function __construct(UuidInterface $uuid_generator) {
39
    $this->uuidGenerator = $uuid_generator;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public static function create(ContainerInterface $container) {
46
    return new static(
47
      $container->get('uuid')
48
    );
49
  }
50
51
  /**
52
   * {@inheritdoc}
53
   */
54
  public function getFormId() {
55
    return 'entity_browser_' . $this->entity_browser->id() . '_form';
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function setEntityBrowser(EntityBrowserInterface $entity_browser) {
62
    $this->entity_browser = $entity_browser;
63
  }
64
65
  /**
66
   * Initializes form state.
67
   *
68
   * @param \Drupal\Core\Form\FormStateInterface
69
   *   Form state object.
70
   */
71
  protected function init(FormStateInterface $form_state) {
72
    // Flag that this form has been initialized.
73
    $form_state->set('entity_form_initialized', TRUE);
74
    if ($this->getRequest()->query->has('uuid')) {
75
      $form_state->set(['entity_browser', 'instance_uuid'], $this->getRequest()->query->get('uuid'));
76
    }
77
    else {
78
      $form_state->set(['entity_browser', 'instance_uuid'], $this->uuidGenerator->generate());
79
    }
80
    $form_state->set(['entity_browser', 'selected_entities'], []);
81
    $form_state->set(['entity_browser', 'selection_completed'], FALSE);
82
  }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function buildForm(array $form, FormStateInterface $form_state) {
88
    // During the initial form build, add this form object to the form state and
89
    // allow for initial preparation before form building and processing.
90
    if (!$form_state->has('entity_form_initialized')) {
91
      $this->init($form_state);
92
    }
93
94
    $form['#attributes']['class'][] = 'entity-browser-form';
95
    $form['#browser_parts'] = [
96
      'widget_selector' => 'widget_selector',
97
      'widget' => 'widget',
98
      'selection_display' => 'selection_display',
99
    ];
100
    $this->entity_browser
101
      ->getWidgetSelector()
102
      ->setDefaultWidget($this->getCurrentWidget($form_state));
103
    $form[$form['#browser_parts']['widget_selector']] = $this->entity_browser
104
      ->getWidgetSelector()
105
      ->getForm($form, $form_state);
106
    $form[$form['#browser_parts']['widget']] = $this->entity_browser
107
      ->getWidgets()
108
      ->get($this->getCurrentWidget($form_state))
109
      ->getForm($form, $form_state, $this->entity_browser->getAdditionalWidgetParameters());
110
111
    $form['actions'] = [
112
      'submit' => [
113
        '#type' => 'submit',
114
        '#value' => $this->entity_browser->getSubmitButtonText(),
115
        '#attributes' => [
116
          'class' => ['is-entity-browser-submit'],
117
        ],
118
      ],
119
    ];
120
121
    $form[$form['#browser_parts']['selection_display']] = $this->entity_browser
122
      ->getSelectionDisplay()
123
      ->getForm($form, $form_state);
124
125
    if ($this->entity_browser->getDisplay() instanceOf DisplayAjaxInterface) {
126
      $this->entity_browser->getDisplay()->addAjax($form);
127
    }
128
129
    return $form;
130
  }
131
132
  /**
133
   * {@inheritdoc}
134
   */
135
  public function validateForm(array &$form, FormStateInterface $form_state) {
136
    $this->entity_browser->getWidgetSelector()->validate($form, $form_state);
137
    $this->entity_browser->getWidgets()->get($this->getCurrentWidget($form_state))->validate($form, $form_state);
138
    $this->entity_browser->getSelectionDisplay()->validate($form, $form_state);
139
  }
140
141
  /**
142
   * {@inheritdoc}
143
   */
144
  public function submitForm(array &$form, FormStateInterface $form_state) {
145
    $original_widget = $this->getCurrentWidget($form_state);
146
    if ($new_widget = $this->entity_browser->getWidgetSelector()->submit($form, $form_state)) {
147
      $this->setCurrentWidget($new_widget, $form_state);
148
    }
149
150
    // Only call widget submit if we didn't change the widget.
151
    if ($original_widget == $this->getCurrentWidget($form_state)) {
152
      $this->entity_browser
153
        ->getWidgets()
154
        ->get($this->getCurrentWidget($form_state))
155
        ->submit($form[$form['#browser_parts']['widget']], $form, $form_state);
156
157
      $this->entity_browser
158
        ->getSelectionDisplay()
159
        ->submit($form, $form_state);
160
    }
161
162
    if (!$this->isSelectionCompleted($form_state)) {
163
      $form_state->setRebuild();
164
    }
165
    else {
166
      $this->entity_browser->getDisplay()->selectionCompleted($this->getSelectedEntities($form_state));
167
    }
168
  }
169
170
  /**
171
   * Returns the widget that is currently selected.
172
   *
173
   * @param \Drupal\Core\Form\FormStateInterface $form_state
174
   *   The current state of the form.
175
   *
176
   * @return string
177
   *   ID of currently selected widget.
178
   */
179
  protected function getCurrentWidget(FormStateInterface $form_state) {
180
    // Do not use has() as that returns TRUE if the value is NULL.
181
    if (!$form_state->get('entity_browser_current_widget')) {
182
      $form_state->set('entity_browser_current_widget', $this->entity_browser->getFirstWidget());
183
    }
184
185
    return $form_state->get('entity_browser_current_widget');
186
  }
187
188
  /**
189
   * Sets widget that is currently active.
190
   *
191
   * @param string $widget
192
   *   New active widget UUID.
193
   * @param \Drupal\Core\Form\FormStateInterface $form_state
194
   *   Form state.
195
   */
196
  protected function setCurrentWidget($widget, FormStateInterface $form_state) {
197
    $form_state->set('entity_browser_current_widget', $widget);
198
  }
199
200
  /**
201
   * Indicates selection is done.
202
   *
203
   * @param \Drupal\Core\Form\FormStateInterface $form_state
204
   *   Form state.
205
   *
206
   * @return bool
207
   *   Indicates selection is done.
208
   */
209
  protected function isSelectionCompleted(FormStateInterface $form_state) {
210
    return (bool) $form_state->get(['entity_browser', 'selection_completed']);
211
  }
212
213
  /**
214
   * Returns currently selected entities.
215
   *
216
   * @param \Drupal\Core\Form\FormStateInterface $form_state
217
   *   Form state.
218
   *
219
   * @return \Drupal\Core\Entity\EntityInterface[]
220
   *   Array of currently selected entities.
221
   */
222
  protected function getSelectedEntities(FormStateInterface $form_state) {
223
    return $form_state->get(['entity_browser', 'selected_entities']);
224
  }
225
226
}
227