GeneralInfoConfig   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 151
Duplicated Lines 7.95 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 12
loc 151
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A create() 0 8 1
A getFormId() 0 3 1
A submitForm() 0 9 1
C buildForm() 12 65 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Drupal\entity_browser\Form;
4
5
use Drupal\Core\Form\FormBase;
6
use Drupal\Core\Form\FormStateInterface;
7
use Drupal\entity_browser\DisplayManager;
8
use Drupal\entity_browser\SelectionDisplayManager;
9
use Drupal\entity_browser\WidgetManager;
10
use Drupal\entity_browser\WidgetSelectorManager;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
 * General information configuration step in entity browser form wizard.
15
 */
16
class GeneralInfoConfig extends FormBase {
17
18
  /**
19
   * Entity browser display plugin manager.
20
   *
21
   * @var \Drupal\entity_browser\DisplayManager
22
   */
23
  protected $displayManager;
24
25
  /**
26
   * Entity browser widget selector plugin manager.
27
   *
28
   * @var \Drupal\entity_browser\WidgetSelectorManager
29
   */
30
  protected $widgetSelectorManager;
31
32
  /**
33
   * Entity browser selection display plugin manager.
34
   *
35
   * @var \Drupal\entity_browser\SelectionDisplayManager
36
   */
37
  protected $selectionDisplayManager;
38
39
  /**
40
   * Entity browser widget plugin manager.
41
   *
42
   * @var \Drupal\entity_browser\WidgetManager
43
   */
44
  protected $widgetManager;
45
46
  /**
47
   * Constructs GeneralInfoConfig form class.
48
   *
49
   * @param \Drupal\entity_browser\DisplayManager $display_manager
50
   *   Entity browser display plugin manager.
51
   * @param \Drupal\entity_browser\WidgetSelectorManager $widget_selector_manager
52
   *   Entity browser widget selector plugin manager.
53
   * @param \Drupal\entity_browser\SelectionDisplayManager $selection_display_manager
54
   *   Entity browser selection display plugin manager.
55
   * @param \Drupal\entity_browser\WidgetManager $widget_manager
56
   *   Entity browser widget plugin manager.
57
   */
58
  function __construct(DisplayManager $display_manager, WidgetSelectorManager $widget_selector_manager, SelectionDisplayManager $selection_display_manager, WidgetManager $widget_manager) {
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...
59
    $this->displayManager = $display_manager;
60
    $this->selectionDisplayManager = $selection_display_manager;
61
    $this->widgetSelectorManager = $widget_selector_manager;
62
    $this->widgetManager = $widget_manager;
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public static function create(ContainerInterface $container) {
69
    return new static(
70
      $container->get('plugin.manager.entity_browser.display'),
71
      $container->get('plugin.manager.entity_browser.widget_selector'),
72
      $container->get('plugin.manager.entity_browser.selection_display'),
73
      $container->get('plugin.manager.entity_browser.widget')
74
    );
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public function getFormId() {
81
    return 'entity_browser_general_info_config_form';
82
  }
83
84
  /**
85
   * {@inheritdoc}
86
   */
87
  public function buildForm(array $form, FormStateInterface $form_state) {
88
    $cached_values = $form_state->getTemporaryValue('wizard');
89
    /** @var \Drupal\entity_browser\EntityBrowserInterface  $entity_browser */
90
    $entity_browser = $cached_values['entity_browser'];
91
92
    if (empty($entity_browser->id())) {
93
      $help_text = '<div class="clearfix eb-help-text"><h2>' . $this->t('Entity Browser creation instructions') . '</h2>';
94
      $help_text .= '<p>' . $this->t('This is a multi-step form. In this first step you need to define the main characteristics of the Entity Browser (in other words, which plugins will be used for each functionality). In the following steps of this wizard, each individual plugin can be configured, when necessary.') . '</p>';
95
      $help_text .= '<p>' . $this->t('You can find more detailed information about creating and configuring Entity Browsers at the <a href="@guide_href" target="_blank">official documentation</a>.', ['@guide_href' => 'https://drupal-media.gitbooks.io/drupal8-guide/content/modules/entity_browser/intro.html']) . '</p>';
96
      $help_text .= '</div>';
97
      $form['help_text'] = [
98
        '#markup' => $help_text,
99
      ];
100
    }
101
102
    $displays = [];
103
    $display_description = $this->t('Choose here how the browser(s) should be presented to the end user. The available plugins are:') . '<ul>';
104 View Code Duplication
    foreach ($this->displayManager->getDefinitions() as $plugin_id => $plugin_definition) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105
      $displays[$plugin_id] = $plugin_definition['label'];
106
      $display_description .= '<li><b>' . $plugin_definition['label'] . ':</b> ' . $plugin_definition['description'] . '</li>';
107
    }
108
    $display_description .= '</ul>';
109
    $form['display'] = [
110
      '#type' => 'select',
111
      '#title' => $this->t('Display plugin'),
112
      '#description' => $display_description,
113
      '#default_value' => $entity_browser->get('display') ? $entity_browser->getDisplay()->getPluginId() : 'modal',
114
      '#options' => $displays,
115
      '#required' => TRUE,
116
    ];
117
118
    $widget_selectors = [];
119
    $widget_description = $this->t('In the last step of the entity browser configuration you can decide how the widgets will be available to the editor. The available plugins are:') . '<ul>';
120 View Code Duplication
    foreach ($this->widgetSelectorManager->getDefinitions() as $plugin_id => $plugin_definition) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
121
      $widget_selectors[$plugin_id] = $plugin_definition['label'];
122
      $widget_description .= '<li><b>' . $plugin_definition['label'] . ':</b> ' . $plugin_definition['description'] . '</li>';
123
    }
124
    $widget_description .= '</ul>';
125
    $form['widget_selector'] = [
126
      '#type' => 'select',
127
      '#title' => $this->t('Widget selector plugin'),
128
      '#description' => $widget_description,
129
      '#default_value' => $entity_browser->get('widget_selector') ? $entity_browser->getWidgetSelector()->getPluginId() : 'tabs',
130
      '#options' => $widget_selectors,
131
      '#required' => TRUE,
132
    ];
133
134
    $selection_display = [];
135
    $selection_description = $this->t('You can optionally allow a "work-in-progress selection zone" to be available to the editor, while still navigating, browsing and selecting the entities. The available plugins are:') . '<ul>';
136 View Code Duplication
    foreach ($this->selectionDisplayManager->getDefinitions() as $plugin_id => $plugin_definition) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
137
      $selection_display[$plugin_id] = $plugin_definition['label'];
138
      $selection_description .= '<li><b>' . $plugin_definition['label'] . ':</b> ' . $plugin_definition['description'] . '</li>';
139
    }
140
    $selection_description .= '</ul>';
141
    $form['selection_display'] = [
142
      '#type' => 'select',
143
      '#title' => $this->t('Selection display plugin'),
144
      '#description' => $selection_description,
145
      '#default_value' => $entity_browser->get('selection_display') ? $entity_browser->getSelectionDisplay()->getPluginId() : 'no_display',
146
      '#options' => $selection_display,
147
      '#required' => TRUE,
148
    ];
149
150
    return $form;
151
  }
152
153
  /**
154
   * {@inheritdoc}
155
   */
156
  public function submitForm(array &$form, FormStateInterface $form_state) {
157
    /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
158
    $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser'];
159
    $entity_browser->setName($form_state->getValue('id'))
160
      ->setLabel($form_state->getValue('label'))
161
      ->setDisplay($form_state->getValue('display'))
162
      ->setWidgetSelector($form_state->getValue('widget_selector'))
163
      ->setSelectionDisplay($form_state->getValue('selection_display'));
164
  }
165
166
}
167