| Conditions | 10 |
| Paths | 11 |
| Total Lines | 80 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 39 | public function existing(EntityInterface $entity, $field_name, $langcode, $view_mode_id, Request $request) { |
||
| 40 | $mid = $request->request->get('mid'); |
||
| 41 | if (!$mid || !is_numeric($mid)) { |
||
| 42 | throw new BadRequestHttpException('MID missing or invalid.'); |
||
| 43 | } |
||
| 44 | |||
| 45 | $field = $this->getField($entity, $field_name, $langcode); |
||
| 46 | $field_validators = $field->getUploadValidators(); |
||
| 47 | $field_settings = $field->getFieldDefinition()->getSettings(); |
||
| 48 | |||
| 49 | // Add upload resolution validation. |
||
| 50 | if ($field_settings['max_resolution'] || $field_settings['min_resolution']) { |
||
| 51 | $field_validators['file_validate_image_resolution'] = [$field_settings['max_resolution'], $field_settings['min_resolution']]; |
||
| 52 | } |
||
| 53 | |||
| 54 | // Attempt to load the image given the field's constraints. |
||
| 55 | $media = Media::load($mid); |
||
| 56 | if (!$media || !$media->bundle() == 'image') { |
||
|
|
|||
| 57 | throw new BadRequestHttpException('Media Entity not found or not of bundle "Image".'); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** @var \Drupal\file\Entity\file $file */ |
||
| 61 | $file = $media->get('image')->first()->get('entity')->getTarget()->getValue(); |
||
| 62 | if ($file) { |
||
| 63 | // Call the validation functions specified by this function's caller. |
||
| 64 | $errors = file_validate($file, $field_validators); |
||
| 65 | |||
| 66 | // Check for errors. |
||
| 67 | if (!empty($errors)) { |
||
| 68 | $message = array( |
||
| 69 | 'error' => array( |
||
| 70 | '#markup' => $this->t('The specified file %name could not be uploaded.', array('%name' => $file->getFilename())), |
||
| 71 | ), |
||
| 72 | 'item_list' => array( |
||
| 73 | '#theme' => 'item_list', |
||
| 74 | '#items' => $errors, |
||
| 75 | ), |
||
| 76 | ); |
||
| 77 | // Return a JSON object containing the errors from Drupal and our |
||
| 78 | // "main_error", which is displayed inside the dropzone area. |
||
| 79 | drupal_set_message(\Drupal::service('renderer')->renderPlain($message), 'error'); |
||
| 80 | $messages = StatusMessages::renderMessages('error'); |
||
| 81 | return new JsonResponse(['errors' => $this->renderer->render($messages), 'main_error' => $this->t('The requested image failed validation.')]); |
||
| 82 | } |
||
| 83 | |||
| 84 | $image = $this->imageFactory->get($file->getFileUri()); |
||
| 85 | |||
| 86 | // Set the value in the Entity to the new file. |
||
| 87 | /** @var \Drupal\file\Plugin\Field\FieldType\FileFieldItemList $field_list */ |
||
| 88 | $value = $entity->$field_name->getValue(); |
||
| 89 | $value[0]['target_id'] = $file->id(); |
||
| 90 | $value[0]['width'] = $image->getWidth(); |
||
| 91 | $value[0]['height'] = $image->getHeight(); |
||
| 92 | $entity->$field_name->setValue($value); |
||
| 93 | |||
| 94 | // Render the new image using the correct formatter settings. |
||
| 95 | $entity_view_mode_ids = array_keys($this->entityManager()->getViewModes($entity->getEntityTypeId())); |
||
| 96 | if (in_array($view_mode_id, $entity_view_mode_ids)) { |
||
| 97 | $output = $entity->$field_name->view($view_mode_id); |
||
| 98 | } |
||
| 99 | else { |
||
| 100 | // Each part of a custom (non-Entity Display) view mode ID is separated |
||
| 101 | // by a dash; the first part must be the module name. |
||
| 102 | $mode_id_parts = explode('-', $view_mode_id, 2); |
||
| 103 | $module = reset($mode_id_parts); |
||
| 104 | $args = [$entity, $field_name, $view_mode_id, $langcode]; |
||
| 105 | $output = $this->moduleHandler()->invoke($module, 'quickedit_render_field', $args); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Save the Entity to tempstore. |
||
| 109 | $this->tempStore->set($entity->uuid(), $entity); |
||
| 110 | |||
| 111 | $data = [ |
||
| 112 | 'fid' => $file->id(), |
||
| 113 | 'html' => $this->renderer->renderRoot($output), |
||
| 114 | ]; |
||
| 115 | return new JsonResponse($data); |
||
| 116 | } |
||
| 117 | else { |
||
| 118 | return new JsonResponse(['main_error' => $this->t('File does not exist.')]); |
||
| 119 | } |
||
| 123 |