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

WidgetsConfig::addWidgetCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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
   * Constructs GeneralInfoConfig form class.
34
   *
35
   * @param \Drupal\entity_browser\DisplayManager $display_manager
0 ignored issues
show
Bug introduced by
There is no parameter named $display_manager. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
   *   Entity browser display plugin manager.
37
   * @param \Drupal\entity_browser\WidgetSelectorManager $widget_selector
0 ignored issues
show
Bug introduced by
There is no parameter named $widget_selector. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
38
   *   Entity browser widget selector plugin manager.
39
   * @param \Drupal\entity_browser\SelectionDisplayManager
40
   *   Entity browser selection display plugin manager.
41
   */
42
  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...
43
    $this->widgetManager = $widget_manager;
44
    $this->tempStore = $temp_store;
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   */
50
  public static function create(ContainerInterface $container) {
51
    return new static(
52
      $container->get('plugin.manager.entity_browser.widget'),
53
      $container->get('user.shared_tempstore')
54
    );
55
  }
56
57
  /**
58
   * {@inheritdoc}
59
   */
60
  public function getFormId() {
61
    return 'entity_browser_widgets_config_form';
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public function buildForm(array $form, FormStateInterface $form_state) {
68
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
69
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
70
71
    $widgets = [];
72
    foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) {
73
      $widgets[$plugin_id] = $plugin_definition['label'];
74
    }
75
    $default_widgets = [];
76
    foreach ($entity_browser->getWidgets() as $widget) {
77
      /** @var \Drupal\entity_browser\WidgetInterface $widget */
78
      $default_widgets[] = $widget->id();
79
    }
80
    $form['widget'] = [
81
      '#type' => 'select',
82
      '#title' => $this->t('Add widget plugin'),
83
      '#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets,
84
      '#ajax' => [
85
        'callback' => [get_class($this), 'addWidgetCallback'],
86
        'wrapper' => 'widgets',
87
        'event' => 'change'
88
      ],
89
      '#executes_submit_callback' => TRUE,
90
      '#submit' => [[get_class($this), 'submitAddWidget']],
91
      '#limit_validation_errors' => [['widget']],
92
    ];
93
    $form_state->unsetValue('widget');
94
95
    $form['widgets'] = [
96
      '#type' => 'table',
97
      '#header' => [
98
        $this->t('Form'),
99
        $this->t('Operations'),
100
        $this->t('Weight'),
101
      ],
102
      '#empty' => $this->t('There are no widgets.'),
103
      '#tabledrag' => [[
104
        'action' => 'order',
105
        'relationship' => 'sibling',
106
        'group' => 'variant-weight',
107
      ]],
108
    ];
109
110
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
111
    foreach ($entity_browser->getWidgets() as $widget) {
112
      $row = [
113
        '#attributes' => [
114
          'class' => ['draggable'],
115
        ],
116
      ];
117
      $row['label'] = [
118
        '#type' => 'textfield',
119
        '#default_value' => $widget->label(),
120
        '#title' => $this->t('Label'),
121
      ];
122
      $row['form'] = [];
123
      $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state);
124
      $row['weight'] = [
125
        '#type' => 'weight',
126
        '#default_value' => $widget->getWeight(),
127
        '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]),
128
        '#title_display' => 'invisible',
129
        '#attributes' => [
130
          'class' => ['variant-weight'],
131
        ],
132
      ];
133
      $form['widgets'][$widget->uuid()] = $row;
134
    }
135
    $form['#attached']['library'][] = 'entity_browser/widgets';
136
    return $form;
137
  }
138
139
  public static function submitAddWidget($form, FormStateInterface $form_state, $tempstore_id = NULL) {
140
    $cached_values = $form_state->getTemporaryValue('wizard');
141
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
142
    $entity_browser = $cached_values['entity_browser'];
143
    $widget = $form_state->getValue('widget');
144
    $entity_browser->addWidget([
145
      'id' => $widget,
146
      'label' => $widget,
147
      'weight' => 0,
148
      // Configuration will be set on the widgets page.
149
      'settings' => [],
150
    ]);
151
    \Drupal::service('user.shared_tempstore')
152
      ->get('entity_browser.config')
153
      ->set($entity_browser->id(), $cached_values);
154
    $form_state->setRebuild();
155
  }
156
157
  public static function addWidgetCallback($form, $form_state) {
158
    return $form['widgets'];
159
  }
160
161
  /**
162
   * {@inheritdoc}
163
   */
164 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...
165
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
166
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
167
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
168
    foreach ($entity_browser->getWidgets() as $widget) {
169
      $widget->validateConfigurationForm($form, $form_state);
170
    }
171
  }
172
173
  /**
174
   * {@inheritdoc}
175
   */
176 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...
177
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
178
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
179
    /** @var \Drupal\entity_browser\WidgetInterface $widget */
180
    foreach ($entity_browser->getWidgets() as $widget) {
181
      $widget->submitConfigurationForm($form, $form_state);
182
    }
183
  }
184
185
}
186