Skip to content
Snippets Groups Projects

Move into a service and add back the test module

Open Eirik Morland requested to merge issue/drupal-2107455:11.x into 11.x
18 unresolved threads

Merge request reports

Members who can merge are allowed to add commits.
Approval is optional
Code Quality is loading
Test summary results are being parsed
Ready to merge by members who can write to the target branch.
  • The source branch is 425 commits behind the target branch.
  • 1 commit will be added to 11.x.
  • Source branch will not be deleted.

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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.
  • 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')
  • 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 {
  • 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 {
  • 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']);
  • 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()
  • 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
  • 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) {
  • Joseph Olstad added 3 commits

    added 3 commits

    • 5b9f60aa - Issue #2107455 by kksandr - In most cases formatted in the style (): array.
    • cdfdc3a0 - Issue #2107455 by kksandr - the entity_field_info tag should be added so that...
    • 5158f6f6 - Issue #2107455 by kksandr - Base fields can also have default images, for...

    Compare with previous version

  • added 1 commit

    • 1712107e - Issue #2107455 by kksandr - the entity_field_info tag should be added so that...

    Compare with previous version

  • added 1 commit

    • cb8ad429 - Revert "Issue #2107455 by kksandr - Base fields can also have default images,...

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    • 799c010d - Issue #2107455 by kksandr: moving access checks to the service and general refactoring

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    • 09218f33 - Issue #2107455 by kksandr: defined constant for default image directory

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • 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());
  • 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.
  • 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.
  • 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.
  • 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.
  • 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 . '/')) {
  • 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) {
  • Matthias Lünemann added 558 commits

    added 558 commits

    Compare with previous version

  • added 1 commit

    Compare with previous version

  • added 1 commit

    • 7a8717c9 - Apply 1 suggestion(s) to 1 file(s)

    Compare with previous version

  • added 1 commit

    • 315df5c2 - Apply 1 suggestion(s) to 1 file(s)

    Compare with previous version

  • added 1 commit

    • 1b6d353c - Apply 1 suggestion(s) to 1 file(s)

    Compare with previous version

  • added 1 commit

    • 5d611270 - Apply 1 suggestion(s) to 1 file(s)

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • kksandr added 340 commits

    added 340 commits

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • 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 {
  • kksandr added 1 commit

    added 1 commit

    • ed4f6f48 - Issue #2107455 by kksandr: replace test procedural hook with hook class

    Compare with previous version

  • kksandr added 1 commit

    added 1 commit

    Compare with previous version

  • nicxvan
    nicxvan @nicxvan started a thread on the diff
  • 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')]
  • kksandr added 81 commits

    added 81 commits

    Compare with previous version

  • Joseph Olstad added 83 commits

    added 83 commits

    Compare with previous version

  • kksandr added 20 commits

    added 20 commits

    Compare with previous version

  • Please register or sign in to reply
    Loading