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

WidgetsConfig::submitAddWidget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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