Move into a service and add back the test module
Merge request reports
Activity
added 1191 commits
-
1f87b9aa...d4483f5c - 1190 commits from branch
project:11.x
- 61ce2a9d - Issue #2107455 by mparker17, ravi.shankar, pooja saraah, yogeshmpawar, KapilV,...
-
1f87b9aa...d4483f5c - 1190 commits from branch
added 1 commit
added 1 commit
143 143 if (str_starts_with($path, 'styles/')) { 144 144 $args = explode('/', $path); 145 145 146 // Discard "styles", style name, and scheme from the path 146 // Discard "styles", style name, and scheme from the path. changed this line in version 16 of the diff
- core/modules/image/src/ImageFieldManager.php 0 → 100644
74 * ] 75 * @code 76 */ 77 public function getDefaultImageFields() : array { 78 $cid = 'image:default_images'; 79 if (!isset($this->cachedDefaults)) { 80 $cache = $this->cache->get($cid); 81 if ($cache) { 82 $this->cachedDefaults = $cache->data; 83 } 84 else { 85 // Save a map of all default image UUIDs and their corresponding field 86 // configuration IDs for quick lookup. 87 $defaults = []; 88 $fields = $this->entityTypeManager 89 ->getStorage('field_config') changed this line in version 5 of the diff
- core/modules/image/src/ImageFieldManager.php 0 → 100644
62 * an image stored in the private file storage. 63 * 64 * @return array 65 * An associative array, where the keys are image file URIs, and the values 66 * are arrays of field configuration IDs which use that image file as their 67 * default image. For example, 68 * 69 * @code [ 70 * 'private://default_images/astronaut.jpg' => [ 71 * 'node.article.field_image', 72 * 'user.user.field_portrait', 73 * ], 74 * ] 75 * @code 76 */ 77 public function getDefaultImageFields() : array { changed this line in version 5 of the diff
- core/modules/image/src/ImageFieldManager.php 0 → 100644
1 <?php 2 3 namespace Drupal\image; 4 5 use Drupal\Core\Cache\CacheBackendInterface; 6 use Drupal\Core\Entity\EntityRepositoryInterface; 7 use Drupal\Core\Entity\EntityTypeManagerInterface; 8 use Drupal\file\FileInterface; 9 10 /** 11 * Provides a service for managing image fields. 12 */ 13 class ImageFieldManager { Shouldn't all services be based on interfaces so they can be decorated or overridden?
Edited by kksandrchanged this line in version 8 of the diff
- core/modules/image/src/ImageFieldManager.php 0 → 100644
104 // Field storage config can also have a default image. 105 $storage_uuid = $field->getFieldStorageDefinition()->getSetting('default_image')['uuid']; 106 if ($storage_uuid) { 107 $file = $this->entityRepository->loadEntityByUuid('file', $storage_uuid); 108 if ($file instanceof FileInterface) { 109 // Use the field config id since that is what we'll be using to 110 // check access in image_file_download(). 111 $defaults[$file->getFileUri()][] = $field->get('id'); 112 } 113 } 114 } 115 } 116 117 // Cache the default image list. 118 $this->cache 119 ->set($cid, $defaults, CacheBackendInterface::CACHE_PERMANENT, ['image_default_images']); changed this line in version 5 of the diff
185 // Default images are displayed as a fallback when an image is not uploaded to 186 // an image field. 187 if (strpos($path, 'default_images/') === 0) { 188 $image = \Drupal::service('image.factory')->get($uri); 189 $user = \Drupal::currentUser(); 190 /** @var Drupal\image\ImageFieldManager $image_field_service */ 191 $image_field_service = \Drupal::service('image.field_manager'); 192 $default_images = $image_field_service->getDefaultImageFields(); 193 $has_access = FALSE; 194 195 // If the image being requested for download is being used as the default 196 // image for any fields, then grant access if the user has 'view' access to 197 // at least one of those fields. 198 if (isset($default_images[$uri])) { 199 foreach ($default_images[$uri] as $field_config_id) { 200 $field = \Drupal::entityTypeManager() changed this line in version 8 of the diff
- core/modules/image/src/ImageFieldManager.php 0 → 100644
17 * 18 * @var \Drupal\Core\Cache\CacheBackendInterface 19 */ 20 protected CacheBackendInterface $cache; 21 22 /** 23 * The entity type manager. 24 * 25 * @var \Drupal\Core\Entity\EntityTypeManagerInterface 26 */ 27 protected EntityTypeManagerInterface $entityTypeManager; 28 29 /** 30 * The entity repository. 31 * 32 * @var \Drupal\Core\Entity\EntityRepositoryInterface changed this line in version 8 of the diff
200 $field = \Drupal::entityTypeManager() 201 ->getStorage('field_config') 202 ->load($field_config_id); 203 $field_definition = $field->getItemDefinition()->getFieldDefinition(); 204 $access_control_handler = \Drupal::entityTypeManager() 205 ->getAccessControlHandler($field->get('entity_type')); 206 207 if ($has_access = $access_control_handler->fieldAccess('view', $field_definition, $user)) { 208 // As long as the user has view access to at least one of the fields, 209 // that uses this image as a default, we can exit this foreach loop, 210 // and grant access. 211 break; 212 } 213 } 214 } 215 if ($image->isValid() && $has_access) { I think it makes sense to check access only after the image is recognized as valid, that is, this should all be under
if ($image->isValid()) { ...
Edited by kksandrchanged this line in version 8 of the diff
added 1 commit
added 1 commit
added 1 commit
added 1 commit
added 1 commit
added 1 commit
added 1 commit
612 612 // Default private image should be displayed when no user supplied image 613 613 // is present. 614 614 $this->assertSession()->responseContains($default_output); 615 616 // Check that the default image itself can be downloaded; i.e.: not just the 617 // HTML markup. 618 $urlForPrivateDefaultImageInNodeField = \Drupal::service('file_url_generator')->generateAbsoluteString($file->getFileUri()); You cannot mix snake_case variables with lowerCamelCase in the same file. In this file snake_case is already used. See https://www.drupal.org/docs/develop/standards/php/php-coding-standards#s-functions-and-variables:
Be consistent; do not mix camelCase and snake_case variable naming inside a file.
changed this line in version 22 of the diff
181 182 return -1; 182 183 } 183 184 185 // Private file access for image fields' default images. 186 // Default images are displayed as a fallback when an image is not uploaded to 187 // an image field. - Comment on lines +185 to +187
2nd phrase should continue inline after the 1st. See https://www.drupal.org/docs/develop/standards/php/api-documentation-and-comment-standards#drupal
Lines containing comments (including docblocks) must wrap as close to 80 characters as possible without going over, with a few exceptions (noted in the Tag Reference below).
If a new paragraph is suggested, then it needs a new empty line between. Seehttps://www.drupal.org/docs/develop/standards/php/api-documentation-and-comment-standards#general
To make a paragraph break in a docblock, leave a blank line (see example above).
changed this line in version 20 of the diff
5 use Drupal\Core\Access\AccessResultInterface; 6 use Drupal\Core\Image\ImageInterface; 7 use Drupal\Core\Session\AccountInterface; 8 9 /** 10 * Provides an interface for an image field manager. 11 */ 12 interface ImageFieldManagerInterface { 13 14 /** 15 * The default image directory. 16 */ 17 public const string DEFAULT_IMAGE_DIRECTORY = 'default_images'; 18 19 /** 20 * Map default values for image fields, and those fields' definitions. changed this line in version 18 of the diff
15 * The default image directory. 16 */ 17 public const string DEFAULT_IMAGE_DIRECTORY = 'default_images'; 18 19 /** 20 * Map default values for image fields, and those fields' definitions. 21 * 22 * @return array<string, \Drupal\Core\Field\FieldDefinitionInterface[]> 23 * An associative array, where the keys are image file URIs, and the values 24 * are arrays of field definitions which use that image file as their 25 * default image. 26 */ 27 public function getDefaultImageFields(): array; 28 29 /** 30 * Check access to a default image. changed this line in version 19 of the diff
181 182 return -1; 182 183 } 183 184 185 // Private file access for image fields' default images. 186 // Default images are displayed as a fallback when an image is not uploaded to 187 // an image field. - Comment on lines +185 to +187
199 // Private file access for image fields' default images. 200 // Default images are displayed as a fallback when an image is not uploaded to 201 // an image field. 199 // Private file access for image fields' default images. Default images are 200 // displayed as a fallback when an image is not uploaded to an image field. changed this line in version 20 of the diff
181 182 return -1; 182 183 } 183 184 185 // Private file access for image fields' default images. 186 // Default images are displayed as a fallback when an image is not uploaded to 187 // an image field. 188 if (str_starts_with($path, ImageFieldManagerInterface::DEFAULT_IMAGE_DIRECTORY . '/')) { Also I wonder if we cannot add this check inside
checkAccessToDefaultImage()
changed this line in version 21 of the diff
- core/modules/image/src/ImageFieldManager.php 0 → 100644
60 $cid = 'image:default_images'; 61 if ($cache = $this->cache->get($cid)) { 62 $this->cachedDefaults = $cache->data; 63 } 64 else { 65 // Save a map of all default image UUIDs and their corresponding field 66 // definitions for quick lookup. 67 $defaults = []; 68 $field_map = $this->entityFieldManager->getFieldMapByFieldType('image'); 69 $cache_tags = [ 70 'image_default_images', 71 'entity_field_info', 72 ]; 73 foreach ($field_map as $entity_type_id => $fields) { 74 $field_storages = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id); 75 foreach ($fields as $field_name => $field_info) { Shouldn't we check the
uri_scheme
from the image storage settings and only deal with images stored inprivate://
?Edited by Claudiu Cristeachanged this line in version 24 of the diff
added 558 commits
-
44df1914...55bab340 - 545 commits from branch
project:11.x
- 55bab340...2c24924a - 3 earlier commits
- aeba3ba3 - Issue #2107455 by kksandr - In most cases formatted in the style (): array.
- bfc18733 - Issue #2107455 by kksandr - the entity_field_info tag should be added so that...
- 55c997da - Issue #2107455 by kksandr - Base fields can also have default images, for...
- b5242df8 - Issue #2107455 by kksandr - the entity_field_info tag should be added so that...
- 193ba6fa - Revert "Issue #2107455 by kksandr - Base fields can also have default images,...
- 3f71c1f4 - Issue #2107455 by kksandr: moving access checks to the service and general refactoring
- f09e20cb - Issue #2107455 by kksandr: update docs
- bd8940af - Issue #2107455 by kksandr: defined constant for default image directory
- a9efad48 - Issue #2107455 by kksandr: added used files to cache tags
- bc6d3ffd - Issue #2107455 by kksandr: move new service into image module
Toggle commit list-
44df1914...55bab340 - 545 commits from branch
added 340 commits
-
e150c95c...fcc93f62 - 339 commits from branch
project:11.x
- 64fc7cab - Merge branch '11.x' into '11.x'
-
e150c95c...fcc93f62 - 339 commits from branch
added 1 commit
11 use Drupal\Core\Entity\EntityRepositoryInterface; 12 use Drupal\Core\Entity\EntityTypeManagerInterface; 13 use Drupal\Core\Extension\ModuleHandlerInterface; 14 use Drupal\Core\Hook\Attribute\Hook; 15 use Drupal\Core\Image\ImageFactory; 16 use Drupal\Core\Session\AccountInterface; 17 use Drupal\Core\StreamWrapper\StreamWrapperManager; 18 use Drupal\image\Controller\ImageStyleDownloadController; 19 use Drupal\image\Plugin\Field\FieldType\ImageItem; 20 use Symfony\Component\DependencyInjection\Attribute\Autowire; 21 22 /** 23 * Implements hook_file_download(). 24 */ 25 #[Hook('file_download')] 26 class ImageDownloadFileHook { 4 * @file 5 * Image field display test for default images in private file storage. 6 */ 7 8 declare(strict_types=1); 9 10 use Drupal\Core\Access\AccessResultInterface; 11 use Drupal\Core\Field\FieldDefinitionInterface; 12 use Drupal\Core\Session\AccountInterface; 13 use Drupal\Core\Field\FieldItemListInterface; 14 use Drupal\Core\Access\AccessResult; 15 16 /** 17 * Implements hook_entity_field_access(). 18 */ 19 function image_field_display_test_default_private_storage_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, ?FieldItemListInterface $items = NULL): AccessResultInterface { changed this line in version 26 of the diff
added 1 commit
21 22 /** 23 * Implements hook_file_download(). 24 */ 25 #[Hook('file_download')] 26 class ImageDownloadFileHook { 27 28 /** 29 * Cache for private default images. 30 * 31 * @var array<string, \Drupal\Core\Field\FieldDefinitionInterface[]> 32 */ 33 protected array $cachedPrivateDefaultImages; 34 35 public function __construct( 36 #[Autowire(service: 'cache.default')] Yes, Drupal does not currently define a default cache service for autowire via the Drupal\Core\Cache\CacheBackendInterface interface.
Edited by kksandr
added 81 commits
-
8540fb8b...101e0606 - 60 commits from branch
project:11.x
- 101e0606...464c4c8b - 11 earlier commits
- a907a804 - Issue #2107455 by kksandr: added used files to cache tags
- aaecf677 - Issue #2107455 by kksandr: move new service into image module
- 43742dce - fix coding standards
- 76e29163 - Apply 1 suggestion(s) to 1 file(s)
- 80d143fb - Apply 1 suggestion(s) to 1 file(s)
- 1df6ca5d - Issue #2107455 by kksandr: fix standards
- 4a9de52e - Issue #2107455 by kksandr: replace new service with hook class
- 6b7a679a - Issue #2107455 by kksandr: fix phpstan
- ccfccdb4 - Issue #2107455 by kksandr: replace test procedural hook with hook class
- a0d48772 - Issue #2107455 by kksandr: fix namespace
Toggle commit list-
8540fb8b...101e0606 - 60 commits from branch
added 83 commits
-
a0d48772...75ea9ab7 - 82 commits from branch
project:11.x
- a5317913 - Issue #2107455 by joseph.olstad, mparker17, ravi.shankar, pooja saraah,...
-
a0d48772...75ea9ab7 - 82 commits from branch
added 20 commits
- a5317913...3aeaa651 - 10 earlier commits
- 5ed3b449 - Issue #2107455 by kksandr: defined constant for default image directory
- 14cdf079 - Issue #2107455 by kksandr: added used files to cache tags
- 495c771b - Issue #2107455 by kksandr: move new service into image module
- bf319fee - fix coding standards
- 3b4563f4 - Apply 1 suggestion(s) to 1 file(s)
- 02612234 - Apply 1 suggestion(s) to 1 file(s)
- 83faa182 - Issue #2107455 by kksandr: fix standards
- 1732f176 - Issue #2107455 by kksandr: replace new service with hook class
- 0ad8ede3 - Issue #2107455 by kksandr: replace test procedural hook with hook class
- 47e414dc - Issue #2107455 by kksandr: fix namespace
Toggle commit list