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 |
||
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) { |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public static function create(ContainerInterface $container) { |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function getFormId() { |
||
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) { |
|
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) { |
|
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) { |
|
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) { |
||
165 | |||
166 | } |
||
167 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.