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