From 766e5c674f48b410b13ead05bc2b3d0d9b3ece81 Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Thu, 5 Jun 2025 10:19:44 -0300 Subject: [PATCH] added entity access check hook from DGI --- islandlives.module | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/islandlives.module b/islandlives.module index f978964..21dbf25 100644 --- a/islandlives.module +++ b/islandlives.module @@ -40,3 +40,35 @@ function islandlives_views_pre_render(ViewExecutable $view) { $view->element['#attached']['library'][] = 'islandlives/unpublished'; } } + +/** + * Implements hook_entity_access(). + */ +function islandlives_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) { + // Only modify access for 'view' and 'download' operations, and only for anonymous users. + if (!($operation === 'view' || $operation === 'download') || !$account->isAnonymous()) { + return \Drupal\Core\Access\AccessResult::neutral(); + } + + if ($entity instanceof \Drupal\media\MediaInterface) { + if (!$entity->isPublished()) { + return \Drupal\Core\Access\AccessResult::forbidden('Media is not published.'); + } + } + elseif ($entity instanceof \Drupal\file\FileInterface) { + // Get all media attached to the files. + $media = \Drupal::database()->select(\Drupal\islandora_hierarchical_access\LUTGeneratorInterface::TABLE_NAME, 'lut') + ->fields('lut', ['mid']) + ->condition("lut.fid", $entity->id()) + ->execute() + ->fetchCol(); + + foreach ($media as $mid) { + $media_entity = \Drupal::entityTypeManager()->getStorage('media')->load($mid); + if ($media_entity && !$media_entity->isPublished()) { + return \Drupal\Core\Access\AccessResult::forbidden('Media is not published.'); + } + } + } +} +