Completed
Pull Request — 8.x-1.x (#130)
by
unknown
07:20
created

WidgetsConfig::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 4
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['widget']['#attached']['library'][] = 'entity_browser/widget';
91
    $form_state->unsetValue('widget');
92
93
    $form['widgets'] = [
94
      '#type' => 'table',
95
      '#header' => [
96
        $this->t('Form'),
97
        $this->t('Operations'),
98
        $this->t('Action'),
99
        $this->t('Weight'),
100
      ],
101
      '#empty' => $this->t('There are no widgets.'),
102
      '#tabledrag' => [[
103
        'action' => 'order',
104
        'relationship' => 'sibling',
105
        'group' => 'variant-weight',
106
      ]],
107
    ];
108
109
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
110
    foreach ($entity_browser->getWidgets() as $widget) {
111
      $row = [
112
        '#attributes' => [
113
          'class' => ['draggable'],
114
        ],
115
      ];
116
      $row['label'] = [
117
        '#type' => 'textfield',
118
        '#default_value' => $widget->label(),
119
        '#title' => $this->t('Label'),
120
      ];
121
      $row['form'] = [];
122
      $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state);
123
124
      $row['remove'] = [
125
        '#type' => 'button',
126
        '#value' => 'Remove',
127
        '#ajax' => [
128
          'callback' => [get_class($this), 'removeWidgetCallback'],
129
          'wrapper' => 'widgets',
130
          'event' => 'click',
131
        ],
132
      ];
133
134
      $row['weight'] = [
135
        '#type' => 'weight',
136
        '#default_value' => $widget->getWeight(),
137
        '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]),
138
        '#title_display' => 'invisible',
139
        '#attributes' => [
140
          'class' => ['variant-weight'],
141
        ],
142
      ];
143
      $form['widgets'][$widget->uuid()] = $row;
144
    }
145
    return $form;
146
  }
147
148
  /**
149
   * @param $form
150
   * @param \Drupal\Core\Form\FormStateInterface $form_state
151
   * @param null $tempstore_id
152
   */
153
  public static function submitAddWidget($form, FormStateInterface $form_state, $tempstore_id = NULL) {
154
    $cached_values = $form_state->getTemporaryValue('wizard');
155
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
156
    $entity_browser = $cached_values['entity_browser'];
157
    $widget = $form_state->getValue('widget');
158
    $entity_browser->addWidget([
159
      'id' => $widget,
160
      'label' => $widget,
161
      'weight' => 0,
162
      // Configuration will be set on the widgets page.
163
      'settings' => [],
164
    ]);
165
    \Drupal::service('user.shared_tempstore')
166
      ->get('entity_browser.config')
167
      ->set($entity_browser->id(), $cached_values);
168
    $form_state->setRebuild();
169
  }
170
171
  /**
172
   * @param $form
173
   * @param $form_state
174
   * @return mixed
175
   */
176
  public static function addWidgetCallback($form, $form_state) {
177
    return $form['widgets'];
178
  }
179
180
  /**
181
   * @param $form
182
   * @param $form_state
183
   */
184
  public static function removeWidgetCallback($form, $form_state) {
185
    $cached_values = $form_state->getTemporaryValue('wizard');
186
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
187
    $entity_browser = $cached_values['entity_browser'];
188
    $widget = $form_state->getValue('widget');
189
    $entity_browser->deleteWidget($widget);
190
    \Drupal::service('user.shared_tempstore')
191
      ->get('entity_browser.config')
192
      ->set($entity_browser->id(), $cached_values);
193
    $form_state->setRebuild();
194
  }
195
196
  /**
197
   * {@inheritdoc}
198
   */
199 View Code Duplication
  public function validateForm(array &$form, FormStateInterface $form_state) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
201
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
202
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
203
    foreach ($entity_browser->getWidgets() as $widget) {
204
      $widget->validateConfigurationForm($form, $form_state);
205
    }
206
  }
207
208
  /**
209
   * {@inheritdoc}
210
   */
211 View Code Duplication
  public function submitForm(array &$form, FormStateInterface $form_state) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
213
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
214
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
215
    foreach ($entity_browser->getWidgets() as $widget) {
216
      $widget->submitConfigurationForm($form, $form_state);
217
    }
218
  }
219
220
}
221