Conditions | 8 |
Paths | 128 |
Total Lines | 65 |
Code Lines | 50 |
Lines | 12 |
Ratio | 18.46 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
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.