Conditions | 4 |
Paths | 8 |
Total Lines | 89 |
Code Lines | 65 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 3 | Features | 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 |
||
56 | public function buildForm(array $form, FormStateInterface $form_state) { |
||
57 | /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
||
58 | $entity_browser = $form_state->getTemporaryValue('wizard')['entity_browser']; |
||
59 | |||
60 | $widgets = []; |
||
61 | foreach ($this->widgetManager->getDefinitions() as $plugin_id => $plugin_definition) { |
||
62 | $widgets[$plugin_id] = $plugin_definition['label']; |
||
63 | } |
||
64 | $default_widgets = []; |
||
65 | foreach ($entity_browser->getWidgets() as $widget) { |
||
66 | /** @var \Drupal\entity_browser\WidgetInterface $widget */ |
||
67 | $default_widgets[] = $widget->id(); |
||
68 | } |
||
69 | $form['widget'] = [ |
||
70 | '#type' => 'select', |
||
71 | '#title' => $this->t('Add widget plugin'), |
||
72 | '#options' => ['_none_' => '- ' . $this->t('Select a widget to add it') . ' -'] + $widgets, |
||
73 | '#ajax' => [ |
||
74 | 'callback' => [get_class($this), 'addWidgetCallback'], |
||
75 | 'wrapper' => 'widgets', |
||
76 | 'event' => 'change' |
||
77 | ], |
||
78 | '#executes_submit_callback' => TRUE, |
||
79 | '#submit' => [[get_class($this), 'submitAddWidget']], |
||
80 | '#limit_validation_errors' => [['widget']], |
||
81 | ]; |
||
82 | $form_state->unsetValue('widget'); |
||
83 | |||
84 | $form['widgets'] = [ |
||
85 | '#type' => 'container', |
||
86 | '#attributes' => ['id' => 'widgets'], |
||
87 | ]; |
||
88 | |||
89 | $form['widgets']['table'] = [ |
||
90 | '#type' => 'table', |
||
91 | '#header' => [ |
||
92 | $this->t('Form'), |
||
93 | $this->t('Operations'), |
||
94 | $this->t('Actions'), |
||
95 | $this->t('Weight'), |
||
96 | ], |
||
97 | '#empty' => $this->t('There are no widgets.'), |
||
98 | '#tabledrag' => [[ |
||
99 | 'action' => 'order', |
||
100 | 'relationship' => 'sibling', |
||
101 | 'group' => 'variant-weight', |
||
102 | ]], |
||
103 | ]; |
||
104 | |||
105 | /** @var \Drupal\entity_browser\WidgetInterface $widget */ |
||
106 | foreach ($entity_browser->getWidgets() as $uuid => $widget) { |
||
107 | $row = [ |
||
108 | '#attributes' => [ |
||
109 | 'class' => ['draggable'], |
||
110 | ], |
||
111 | ]; |
||
112 | $row['label'] = [ |
||
113 | '#type' => 'textfield', |
||
114 | '#default_value' => $widget->label(), |
||
115 | '#title' => $this->t('Label'), |
||
116 | ]; |
||
117 | $row['form'] = []; |
||
118 | $row['form'] = $widget->buildConfigurationForm($row['form'], $form_state); |
||
119 | $row['remove'] = [ |
||
120 | '#type' => 'submit', |
||
121 | '#value' => $this->t('Delete'), |
||
122 | '#name' => 'remove' . $uuid, |
||
123 | '#ajax' => [ |
||
124 | 'callback' => [get_class($this), 'addWidgetCallback'], |
||
125 | 'wrapper' => 'widgets', |
||
126 | 'event' => 'click' |
||
127 | ], |
||
128 | '#executes_submit_callback' => TRUE, |
||
129 | '#submit' => [[get_class($this), 'submitDeleteWidget']], |
||
130 | '#arguments' => $uuid, |
||
131 | ]; |
||
132 | $row['weight'] = [ |
||
133 | '#type' => 'weight', |
||
134 | '#default_value' => $widget->getWeight(), |
||
135 | '#title' => $this->t('Weight for @widget widget', ['@widget' => $widget->label()]), |
||
136 | '#title_display' => 'invisible', |
||
137 | '#attributes' => [ |
||
138 | 'class' => ['variant-weight'], |
||
139 | ], |
||
140 | ]; |
||
141 | $form['widgets']['table'][$uuid] = $row; |
||
142 | } |
||
143 | return $form; |
||
144 | } |
||
145 | |||
223 |
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.