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

GeneralInfoConfig::buildForm()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 43
rs 6.7273
cc 7
eloc 31
nc 64
nop 2
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\entity_browser\Form\GeneralInfoConfig.
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\DisplayManager;
13
use Drupal\entity_browser\SelectionDisplayManager;
14
use Drupal\entity_browser\WidgetManager;
15
use Drupal\entity_browser\WidgetSelectorManager;
16
use Drupal\user\SharedTempStore;
17
use Drupal\user\SharedTempStoreFactory;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
class GeneralInfoConfig extends FormBase {
21
22
  /**
23
   * Entity browser display plugin manager.
24
   *
25
   * @var \Drupal\entity_browser\DisplayManager
26
   */
27
  protected $displayManager;
28
29
  /**
30
   * Entity browser widget selector plugin manager.
31
   *
32
   * @var \Drupal\entity_browser\WidgetSelectorManager
33
   */
34
  protected $widgetSelectorManager;
35
36
  /**
37
   * Entity browser selection display plugin manager.
38
   *
39
   * @var \Drupal\entity_browser\SelectionDisplayManager
40
   */
41
  protected $selectionDisplayManager;
42
43
  /**
44
   * Entity browser widget plugin manager.
45
   *
46
   * @var \Drupal\entity_browser\WidgetManager
47
   */
48
  protected $widgetManager;
49
50
  /**
51
   * Tempstore Factory for keeping track of values in each step of the wizard.
52
   *
53
   * @var \Drupal\user\SharedTempStoreFactory
54
   */
55
  protected $tempstore;
56
57
  protected $tempstoreId;
58
  protected $machineName;
59
  /**
60
   * Constructs GeneralInfoConfig form class.
61
   *
62
   * @param \Drupal\entity_browser\DisplayManager $display_manager
63
   *   Entity browser display plugin manager.
64
   * @param \Drupal\entity_browser\WidgetSelectorManager $widget_selector
0 ignored issues
show
Documentation introduced by
There is no parameter named $widget_selector. Did you maybe mean $widget_selector_manager?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
65
   *   Entity browser widget selector plugin manager.
66
   * @param \Drupal\entity_browser\SelectionDisplayManager
67
   *   Entity browser selection display plugin manager.
68
   */
69
  function __construct(DisplayManager $display_manager, WidgetSelectorManager $widget_selector_manager, SelectionDisplayManager $selection_display_manager, 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...
70
    $this->displayManager = $display_manager;
71
    $this->selectionDisplayManager = $selection_display_manager;
72
    $this->widgetSelectorManager = $widget_selector_manager;
73
    $this->widgetManager = $widget_manager;
74
    $this->tempStore = $temp_store;
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public static function create(ContainerInterface $container) {
81
    return new static(
82
      $container->get('plugin.manager.entity_browser.display'),
83
      $container->get('plugin.manager.entity_browser.widget_selector'),
84
      $container->get('plugin.manager.entity_browser.selection_display'),
85
      $container->get('plugin.manager.entity_browser.widget'),
86
      $container->get('user.shared_tempstore')
87
    );
88
  }
89
90
  /**
91
   * {@inheritdoc}
92
   */
93
  public function getFormId() {
94
    return 'entity_browser_general_info_config_form';
95
  }
96
97
  /**
98
   * {@inheritdoc}
99
   */
100
  public function buildForm(array $form, FormStateInterface $form_state) {
101
    $cached_values = $form_state->getTemporaryValue('wizard');
102
    /** @var \Drupal\entity_browser\EntityBrowserInterface  $entity_browser */
103
    $entity_browser = $cached_values['entity_browser'];
104
105
    $displays = [];
106
    foreach ($this->displayManager->getDefinitions() as $plugin_id => $plugin_definition) {
107
      $displays[$plugin_id] = $plugin_definition['label'];
108
    }
109
    $form['display'] = [
110
      '#type' => 'select',
111
      '#title' => $this->t('Display plugin'),
112
      '#default_value' => $entity_browser->get('display') ? $entity_browser->getDisplay()->getPluginId() : NULL,
113
      '#options' => $displays,
114
      '#required' => TRUE,
115
    ];
116
117
    $widget_selectors = [];
118
    foreach ($this->widgetSelectorManager->getDefinitions() as $plugin_id => $plugin_definition) {
119
      $widget_selectors[$plugin_id] = $plugin_definition['label'];
120
    }
121
    $form['widget_selector'] = [
122
      '#type' => 'select',
123
      '#title' => $this->t('Widget selector plugin'),
124
      '#default_value' => $entity_browser->get('widget_selector') ? $entity_browser->getWidgetSelector()->getPluginId() : NULL,
125
      '#options' => $widget_selectors,
126
      '#required' => TRUE,
127
    ];
128
129
    $selection_display = [];
130
    foreach ($this->selectionDisplayManager->getDefinitions() as $plugin_id => $plugin_definition) {
131
      $selection_display[$plugin_id] = $plugin_definition['label'];
132
    }
133
    $form['selection_display'] = [
134
      '#type' => 'select',
135
      '#title' => $this->t('Selection display plugin'),
136
      '#default_value' => $entity_browser->get('selection_display') ? $entity_browser->getSelectionDisplay()->getPluginId() : NULL,
137
      '#options' => $selection_display,
138
      '#required' => TRUE,
139
    ];
140
141
    return $form;
142
  }
143
144
  /**
145
   * {@inheritdoc}
146
   */
147
  public function submitForm(array &$form, FormStateInterface $form_state) {
148
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
149
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
150
    $entity_browser->setName($form_state->getValue('id'))
151
      ->setLabel($form_state->getValue('label'))
152
      ->setDisplay($form_state->getValue('display'))
153
      ->setWidgetSelector($form_state->getValue('widget_selector'))
154
      ->setSelectionDisplay($form_state->getValue('selection_display'));
155
156
    // TODO must be able to select single widget multiple times. We are adding
157
    // them which ends up with duplicates if we visit first step multiple times.
158
    /*$entity_browser->addWidget([
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
159
      'id' => 'upload',
160
      'label' => 'upload',
161
      'weight' => 0,
162
      // Configuration will be set on the widgets page.
163
      'settings' => [],
164
    ]);*/
165
166
  }
167
168
}
169