|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_frontend\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
6
|
|
|
use Drupal\Core\Render\Element\StatusMessages; |
|
7
|
|
|
use Drupal\image\Controller\QuickEditImageController; |
|
8
|
|
|
use Drupal\media_entity\Entity\Media; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Returns responses for custom Quickedit Image module routes. |
|
15
|
|
|
*/ |
|
16
|
|
|
class QuickEditImageBrowserController extends QuickEditImageController { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Returns a JSON object representing the existing file, or validation |
|
20
|
|
|
* errors. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
|
23
|
|
|
* The entity of which an image field is being rendered. |
|
24
|
|
|
* @param string $field_name |
|
25
|
|
|
* The name of the (image) field that is being rendered |
|
26
|
|
|
* @param string $langcode |
|
27
|
|
|
* The language code of the field that is being rendered. |
|
28
|
|
|
* @param string $view_mode_id |
|
29
|
|
|
* The view mode of the field that is being rendered. |
|
30
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
31
|
|
|
* The current request object. |
|
32
|
|
|
* |
|
33
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
34
|
|
|
* The Ajax response. |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException |
|
37
|
|
|
* Throws an exception if the request is invalid. |
|
38
|
|
|
*/ |
|
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
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.