Conditions | 14 |
Paths | 14 |
Total Lines | 120 |
Code Lines | 75 |
Lines | 8 |
Ratio | 6.67 % |
Changes | 17 | ||
Bugs | 1 | Features | 1 |
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 |
||
209 | function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
||
|
|||
210 | $entity_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type'); |
||
211 | $entity_storage = $this->entityManager->getStorage($entity_type); |
||
212 | $field_widget_display = $this->fieldDisplayManager->createInstance( |
||
213 | $this->getSetting('field_widget_display'), |
||
214 | $this->getSetting('field_widget_display_settings') + ['entity_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type')] |
||
215 | ); |
||
216 | |||
217 | $ids = []; |
||
218 | $entities = []; |
||
219 | if (($trigger = $form_state->getTriggeringElement()) && in_array($this->fieldDefinition->getName(), $trigger['#parents'])) { |
||
220 | // Submit was triggered by hidden "target_id" element when entities were |
||
221 | // added via entity browser. |
||
222 | View Code Duplication | if (!empty($trigger['#ajax']['event']) && $trigger['#ajax']['event'] == 'entity_browser_value_updated') { |
|
223 | $parents = $trigger['#parents']; |
||
224 | } |
||
225 | // Submit was triggered by one of the "Remove" buttons. We need to walk |
||
226 | // few levels up to read value of "target_id" element. |
||
227 | elseif ($trigger['#type'] == 'submit' && strpos($trigger['#name'], $this->fieldDefinition->getName() . '_remove_') === 0) { |
||
228 | $parents = array_merge(array_slice($trigger['#parents'], 0, -4), ['target_id']); |
||
229 | } |
||
230 | |||
231 | if (isset($parents) && $value = $form_state->getValue($parents)) { |
||
232 | $ids = explode(' ', $value); |
||
233 | $entities = $entity_storage->loadMultiple($ids); |
||
234 | } |
||
235 | } |
||
236 | // We are loading for for the first time so we need to load any existing |
||
237 | // values that might already exist on the entity. Also, remove any leftover |
||
238 | // data from removed entity references. |
||
239 | else { |
||
240 | foreach ($items as $item) { |
||
241 | $entity = $entity_storage->load($item->target_id); |
||
242 | if (!empty($entity)) { |
||
243 | $entities[$item->target_id] = $entity; |
||
244 | } |
||
245 | } |
||
246 | $ids = array_keys($entities); |
||
247 | } |
||
248 | $ids = array_filter($ids); |
||
249 | |||
250 | $hidden_id = Html::getUniqueId('edit-' . $this->fieldDefinition->getName() . '-target-id'); |
||
251 | $details_id = Html::getUniqueId('edit-' . $this->fieldDefinition->getName()); |
||
252 | /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
||
253 | $entity_browser = $this->entityManager->getStorage('entity_browser')->load($this->getSetting('entity_browser')); |
||
254 | |||
255 | $element += [ |
||
256 | '#id' => $details_id, |
||
257 | '#type' => 'details', |
||
258 | '#open' => !empty($ids), |
||
259 | 'target_id' => [ |
||
260 | '#type' => 'hidden', |
||
261 | '#id' => $hidden_id, |
||
262 | // We need to repeat ID here as it is otherwise skipped when rendering. |
||
263 | '#attributes' => ['id' => $hidden_id], |
||
264 | '#default_value' => $ids, |
||
265 | // #ajax is officially not supported for hidden elements but if we |
||
266 | // specify event manually it works. |
||
267 | '#ajax' => [ |
||
268 | 'callback' => [get_class($this), 'updateWidgetCallback'], |
||
269 | 'wrapper' => $details_id, |
||
270 | 'event' => 'entity_browser_value_updated', |
||
271 | ], |
||
272 | ], |
||
273 | ]; |
||
274 | |||
275 | $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); |
||
276 | if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || count($ids) < $cardinality) { |
||
277 | $element['entity_browser'] = $entity_browser->getDisplay()->displayEntityBrowser(); |
||
278 | $element['#attached']['library'][] = 'entity_browser/entity_reference'; |
||
279 | $element['#attached']['drupalSettings']['entity_browser'] = [ |
||
280 | $entity_browser->getDisplay()->getUuid() => [ |
||
281 | 'cardinality' => $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(), |
||
282 | 'selector' => '#'.$element['target_id']['#attributes']['id'], |
||
283 | ] |
||
284 | ]; |
||
285 | } |
||
286 | |||
287 | $field_parents = $element['#field_parents']; |
||
288 | |||
289 | $element['current'] = [ |
||
290 | '#theme_wrappers' => ['container'], |
||
291 | '#attributes' => ['class' => ['entities-list']], |
||
292 | 'items' => array_map( |
||
293 | function($id) use ($entity_storage, $field_widget_display, $details_id, $field_parents, $entities) { |
||
294 | $entity = $entities[$id]; |
||
295 | |||
296 | $display = $field_widget_display->view($entity); |
||
297 | if (is_string($display)) { |
||
298 | $display = ['#markup' => $display]; |
||
299 | } |
||
300 | |||
301 | return [ |
||
302 | '#theme_wrappers' => ['container'], |
||
303 | '#attributes' => [ |
||
304 | 'class' => ['item-container'], |
||
305 | 'data-entity-id' => $entity->id() |
||
306 | ], |
||
307 | 'display' => $display, |
||
308 | 'remove_button' => [ |
||
309 | '#type' => 'submit', |
||
310 | '#value' => $this->t('Remove'), |
||
311 | '#ajax' => [ |
||
312 | 'callback' => [get_class($this), 'updateWidgetCallback'], |
||
313 | 'wrapper' => $details_id, |
||
314 | ], |
||
315 | '#submit' => [[get_class($this), 'removeItemSubmit']], |
||
316 | '#name' => $this->fieldDefinition->getName() . '_remove_' . $id, |
||
317 | '#limit_validation_errors' => [array_merge($field_parents, [$this->fieldDefinition->getName()])], |
||
318 | '#attributes' => ['data-entity-id' => $id], |
||
319 | ] |
||
320 | ]; |
||
321 | |||
322 | }, |
||
323 | $ids |
||
324 | ), |
||
325 | ]; |
||
326 | |||
327 | return $element; |
||
328 | } |
||
329 | |||
390 |
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.