Completed
Pull Request — 8.x-1.x (#127)
by Janez
04:27
created

EntityBrowserForm::getCurrentWidget()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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