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