Completed
Pull Request — 8.x-1.x (#137)
by
unknown
03:53
created

WidgetsConfig::submitAddWidget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 2
b 1
f 0
cc 1
eloc 15
nc 1
nop 3
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\entity_browser\Form\WidgetsConfig.
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\WidgetManager;
13
use Drupal\user\SharedTempStoreFactory;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
16
class WidgetsConfig extends FormBase {
17
18
  /**
19
   * Entity browser widget plugin manager.
20
   *
21
   * @var \Drupal\entity_browser\WidgetManager
22
   */
23
  protected $widgetManager;
24
25
  /**
26
   * Tempstore Factory for keeping track of values in each step of the wizard.
27
   *
28
   * @var \Drupal\user\SharedTempStoreFactory
29
   */
30
  protected $tempStore;
31
32
  /**
33
   * WidgetsConfig constructor.
34
   * @param \Drupal\entity_browser\WidgetManager $widget_manager
35
   * @param \Drupal\user\SharedTempStoreFactory $temp_store
36
   * @param null $tempstore_id
37
   * @param null $machine_name
38
   */
39
  function __construct(WidgetManager $widget_manager, SharedTempStoreFactory $temp_store, $tempstore_id = NULL, $machine_name = NULL) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
40
    $this->widgetManager = $widget_manager;
41
    $this->tempStore = $temp_store;
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47
  public static function create(ContainerInterface $container) {
48
    return new static(
49
      $container->get('plugin.manager.entity_browser.widget'),
50
      $container->get('user.shared_tempstore')
51
    );
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
  public function getFormId() {
58
    return 'entity_browser_widgets_config_form';
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function buildForm(array $form, FormStateInterface $form_state) {
65
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
66
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
67
68
    $widgets = [];
69
    foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) {
70
      $widgets[$plugin_id] = $plugin_definition['label'];
71
    }
72
    $default_widgets = [];
73
    foreach ($entity_browser->getWidgets() as $widget) {
74
      /** @var \Drupal\entity_browser\WidgetInterface $widget */
75
      $default_widgets[] = $widget->id();
76
    }
77
    $form['widget'] = [
78
      '#type' => 'select',
79
      '#title' => $this->t('Add widget plugin'),
80
      '#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets,
81
      '#ajax' => [
82
        'callback' => [get_class($this), 'addWidgetCallback'],
83
        'wrapper' => 'widgets',
84
        'event' => 'change'
85
      ],
86
      '#executes_submit_callback' => TRUE,
87
      '#submit' => [[get_class($this), 'submitAddWidget']],
88
      '#limit_validation_errors' => [['widget']],
89
    ];
90
    $form_state->unsetValue('widget');
91
92
    $form['widgets'] = [
93
      '#type' => 'container',
94
      '#attributes' => ['id' => 'widgets'],
95
    ];
96
97
    $form['widgets']['table'] = [
98
      '#type' => 'table',
99
      '#header' => [
100
        $this->t('Form'),
101
        $this->t('Operations'),
102
        $this->t('Remove'),
103
        $this->t('Weight'),
104
      ],
105
      '#empty' => $this->t('There are no widgets.'),
106
      '#tabledrag' => [[
107
        'action' => 'order',
108
        'relationship' => 'sibling',
109
        'group' => 'variant-weight',
110
      ]],
111
    ];
112
113
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
114
    foreach ($entity_browser->getWidgets() as $widget) {
115
      $row = [
116
        '#attributes' => [
117
          'class' => ['draggable'],
118
        ],
119
      ];
120
      $row['label'] = [
121
        '#type' => 'textfield',
122
        '#default_value' => $widget->label(),
123
        '#title' => $this->t('Label'),
124
      ];
125
      $row['form'] = [];
126
      $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state);
127
      $row['remove'] = [
128
        '#type' => 'submit',
129
        '#value' => $this->t('Delete'),
130
        '#ajax' => [
131
          'callback' => [get_class($this), 'addWidgetCallback'],
132
          'wrapper' => 'widgets',
133
          'event' => 'click'
134
        ],
135
        '#executes_submit_callback' => TRUE,
136
        '#submit' => [[get_class($this), 'submitDeleteWidget']],
137
        '#arguments' => $widget->uuid(),
138
      ];
139
      $row['weight'] = [
140
        '#type' => 'weight',
141
        '#default_value' => $widget->getWeight(),
142
        '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]),
143
        '#title_display' => 'invisible',
144
        '#attributes' => [
145
          'class' => ['variant-weight'],
146
        ],
147
      ];
148
      $form['widgets']['table'][$widget->uuid()] = $row;
149
    }
150
    return $form;
151
  }
152
153
  /**
154
   * @param $form
155
   * @param \Drupal\Core\Form\FormStateInterface $form_state
156
   * @param null $tempstore_id
157
   */
158
  public static function submitAddWidget($form, FormStateInterface $form_state, $tempstore_id = NULL) {
159
    $cached_values = $form_state->getTemporaryValue('wizard');
160
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
161
    $entity_browser = $cached_values['entity_browser'];
162
    $widgets_num = count($entity_browser->getWidgets());
163
    $widget = $form_state->getValue('widget');
164
    $weight = $widgets_num + 1;
165
    $entity_browser->addWidget([
166
      'id' => $widget,
167
      'label' => $widget,
168
      'weight' => $weight,
169
      // Configuration will be set on the widgets page.
170
      'settings' => [],
171
    ]);
172
    \Drupal::service('user.shared_tempstore')
173
      ->get('entity_browser.config')
174
      ->set($entity_browser->id(), $cached_values);
175
    $form_state->setRebuild();
176
  }
177
178
  /**
179
   * @param $form
180
   * @param \Drupal\Core\Form\FormStateInterface $form_state
181
   */
182
  public static function submitDeleteWidget($form, FormStateInterface $form_state) {
183
    $cached_values = $form_state->getTemporaryValue('wizard');
184
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
185
    $entity_browser = $cached_values['entity_browser'];
186
    $entity_browser->deleteWidget($entity_browser->getWidget($form_state->getTriggeringElement()['#arguments']));
187
    \Drupal::service('user.shared_tempstore')
188
      ->get('entity_browser.config')
189
      ->set($entity_browser->id(), $cached_values);
190
    $form_state->setRebuild();
191
  }
192
193
  /**
194
   * @param $form
195
   * @param $form_state
196
   * @return mixed
197
   */
198
  public static function addWidgetCallback($form, $form_state) {
199
    return $form['widgets'];
200
  }
201
202
  /**
203
   * {@inheritdoc}
204
   */
205
  public function validateForm(array &$form, FormStateInterface $form_state) {
206
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
207
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
208
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
209
    foreach ($entity_browser->getWidgets() as $widget) {
210
      $widget->validateConfigurationForm($form, $form_state);
211
    }
212
  }
213
214
  /**
215
   * {@inheritdoc}
216
   */
217
  public function submitForm(array &$form, FormStateInterface $form_state) {
218
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
219
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
220
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
221
    foreach ($entity_browser->getWidgets() as $key=>$widget) {
222
      $widget->submitConfigurationForm($form, $form_state);
223
      $widget->setWeight($form_state->getValue('table')[$key]['weight']);
224
    }
225
  }
226
227
}
228