Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/image/src/Controller/QuickEditImageController.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
3 namespace Drupal\image\Controller; | 3 namespace Drupal\image\Controller; |
4 | 4 |
5 use Drupal\Core\Cache\CacheableJsonResponse; | 5 use Drupal\Core\Cache\CacheableJsonResponse; |
6 use Drupal\Core\Controller\ControllerBase; | 6 use Drupal\Core\Controller\ControllerBase; |
7 use Drupal\Core\Entity\ContentEntityInterface; | 7 use Drupal\Core\Entity\ContentEntityInterface; |
8 use Drupal\Core\Entity\EntityDisplayRepositoryInterface; | |
8 use Drupal\Core\Entity\EntityInterface; | 9 use Drupal\Core\Entity\EntityInterface; |
10 use Drupal\Core\File\FileSystemInterface; | |
9 use Drupal\Core\Image\ImageFactory; | 11 use Drupal\Core\Image\ImageFactory; |
10 use Drupal\Core\Render\Element\StatusMessages; | 12 use Drupal\Core\Render\Element\StatusMessages; |
11 use Drupal\Core\Render\RendererInterface; | 13 use Drupal\Core\Render\RendererInterface; |
12 use Drupal\image\Plugin\Field\FieldType\ImageItem; | 14 use Drupal\image\Plugin\Field\FieldType\ImageItem; |
13 use Drupal\Core\TempStore\PrivateTempStoreFactory; | 15 use Drupal\Core\TempStore\PrivateTempStoreFactory; |
40 * @var \Drupal\Core\Image\ImageFactory | 42 * @var \Drupal\Core\Image\ImageFactory |
41 */ | 43 */ |
42 protected $imageFactory; | 44 protected $imageFactory; |
43 | 45 |
44 /** | 46 /** |
47 * The entity display repository service. | |
48 * | |
49 * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface | |
50 */ | |
51 protected $entityDisplayRepository; | |
52 | |
53 /** | |
54 * The file system. | |
55 * | |
56 * @var \Drupal\Core\File\FileSystemInterface | |
57 */ | |
58 protected $fileSystem; | |
59 | |
60 /** | |
45 * Constructs a new QuickEditImageController. | 61 * Constructs a new QuickEditImageController. |
46 * | 62 * |
47 * @param \Drupal\Core\Render\RendererInterface $renderer | 63 * @param \Drupal\Core\Render\RendererInterface $renderer |
48 * The renderer. | 64 * The renderer. |
49 * @param \Drupal\Core\Image\ImageFactory $image_factory | 65 * @param \Drupal\Core\Image\ImageFactory $image_factory |
50 * The image factory. | 66 * The image factory. |
51 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory | 67 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory |
52 * The tempstore factory. | 68 * The tempstore factory. |
53 */ | 69 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository |
54 public function __construct(RendererInterface $renderer, ImageFactory $image_factory, PrivateTempStoreFactory $temp_store_factory) { | 70 * The entity display repository service. |
71 * @param \Drupal\Core\File\FileSystemInterface $file_system | |
72 * The file system. | |
73 */ | |
74 public function __construct(RendererInterface $renderer, ImageFactory $image_factory, PrivateTempStoreFactory $temp_store_factory, EntityDisplayRepositoryInterface $entity_display_repository = NULL, FileSystemInterface $file_system = NULL) { | |
55 $this->renderer = $renderer; | 75 $this->renderer = $renderer; |
56 $this->imageFactory = $image_factory; | 76 $this->imageFactory = $image_factory; |
57 $this->tempStore = $temp_store_factory->get('quickedit'); | 77 $this->tempStore = $temp_store_factory->get('quickedit'); |
78 if (!$entity_display_repository) { | |
79 @trigger_error('The entity_display.repository service must be passed to QuickEditImageController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); | |
80 $entity_display_repository = \Drupal::service('entity_display.repository'); | |
81 } | |
82 $this->entityDisplayRepository = $entity_display_repository; | |
83 if (!$file_system) { | |
84 @trigger_error('The file_system service must be passed to QuickEditImageController::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/3006851.', E_USER_DEPRECATED); | |
85 $file_system = \Drupal::service('file_system'); | |
86 } | |
87 $this->fileSystem = $file_system; | |
58 } | 88 } |
59 | 89 |
60 /** | 90 /** |
61 * {@inheritdoc} | 91 * {@inheritdoc} |
62 */ | 92 */ |
63 public static function create(ContainerInterface $container) { | 93 public static function create(ContainerInterface $container) { |
64 return new static( | 94 return new static( |
65 $container->get('renderer'), | 95 $container->get('renderer'), |
66 $container->get('image.factory'), | 96 $container->get('image.factory'), |
67 $container->get('tempstore.private') | 97 $container->get('tempstore.private'), |
98 $container->get('entity_display.repository'), | |
99 $container->get('file_system') | |
68 ); | 100 ); |
69 } | 101 } |
70 | 102 |
71 /** | 103 /** |
72 * Returns JSON representing the new file upload, or validation errors. | 104 * Returns JSON representing the new file upload, or validation errors. |
93 if ($field_settings['max_resolution'] || $field_settings['min_resolution']) { | 125 if ($field_settings['max_resolution'] || $field_settings['min_resolution']) { |
94 $field_validators['file_validate_image_resolution'] = [$field_settings['max_resolution'], $field_settings['min_resolution']]; | 126 $field_validators['file_validate_image_resolution'] = [$field_settings['max_resolution'], $field_settings['min_resolution']]; |
95 } | 127 } |
96 | 128 |
97 // Create the destination directory if it does not already exist. | 129 // Create the destination directory if it does not already exist. |
98 if (isset($destination) && !file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) { | 130 if (isset($destination) && !$this->fileSystem->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY)) { |
99 return new JsonResponse(['main_error' => $this->t('The destination directory could not be created.'), 'errors' => '']); | 131 return new JsonResponse(['main_error' => $this->t('The destination directory could not be created.'), 'errors' => '']); |
100 } | 132 } |
101 | 133 |
102 // Attempt to save the image given the field's constraints. | 134 // Attempt to save the image given the field's constraints. |
103 $result = file_save_upload('image', $field_validators, $destination); | 135 $result = file_save_upload('image', $field_validators, $destination); |
113 $value[0]['width'] = $image->getWidth(); | 145 $value[0]['width'] = $image->getWidth(); |
114 $value[0]['height'] = $image->getHeight(); | 146 $value[0]['height'] = $image->getHeight(); |
115 $entity->$field_name->setValue($value); | 147 $entity->$field_name->setValue($value); |
116 | 148 |
117 // Render the new image using the correct formatter settings. | 149 // Render the new image using the correct formatter settings. |
118 $entity_view_mode_ids = array_keys($this->entityManager()->getViewModes($entity->getEntityTypeId())); | 150 $entity_view_mode_ids = array_keys($this->entityDisplayRepository->getViewModes($entity->getEntityTypeId())); |
119 if (in_array($view_mode_id, $entity_view_mode_ids, TRUE)) { | 151 if (in_array($view_mode_id, $entity_view_mode_ids, TRUE)) { |
120 $output = $entity->$field_name->view($view_mode_id); | 152 $output = $entity->$field_name->view($view_mode_id); |
121 } | 153 } |
122 else { | 154 else { |
123 // Each part of a custom (non-Entity Display) view mode ID is separated | 155 // Each part of a custom (non-Entity Display) view mode ID is separated |