diff --git a/islandora.services.yml b/islandora.services.yml index b88d186b..b08e0699 100644 --- a/islandora.services.yml +++ b/islandora.services.yml @@ -3,7 +3,7 @@ services: islandora.eventgenerator: class: Drupal\islandora\EventGenerator\EventGenerator - arguments: ['@language_manager', '@islandora.media_source_service'] + arguments: ['@islandora.utils', '@islandora.media_source_service'] islandora.stomp: class: Stomp\StatefulStomp factory: ['Drupal\islandora\StompFactory', create] @@ -16,12 +16,12 @@ services: - { name: event_subscriber } islandora.media_link_header_subscriber: class: Drupal\islandora\EventSubscriber\MediaLinkHeaderSubscriber - arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@language_manager'] + arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@islandora.utils'] tags: - { name: event_subscriber } islandora.node_link_header_subscriber: class: Drupal\islandora\EventSubscriber\NodeLinkHeaderSubscriber - arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@language_manager', '@islandora.utils'] + arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@islandora.utils'] tags: - { name: event_subscriber } islandora.admin_view_route_subscriber: @@ -51,7 +51,7 @@ services: arguments: ['@entity_type.manager', '@current_user', '@language_manager', '@entity.query', '@file_system', '@islandora.utils'] islandora.utils: class: Drupal\islandora\IslandoraUtils - arguments: ['@entity_type.manager', '@entity_field.manager', '@entity.query', '@context.manager', '@flysystem_factory'] + arguments: ['@entity_type.manager', '@entity_field.manager', '@entity.query', '@context.manager', '@flysystem_factory', '@language_manager'] islandora.gemini.client: class: Islandora\Crayfish\Commons\Client\GeminiClient factory: ['Drupal\islandora\GeminiClientFactory', create] diff --git a/modules/islandora_audio/tests/src/Functional/GenerateAudioDerivativeTest.php b/modules/islandora_audio/tests/src/Functional/GenerateAudioDerivativeTest.php index 9c7f4e87..bcd78f70 100644 --- a/modules/islandora_audio/tests/src/Functional/GenerateAudioDerivativeTest.php +++ b/modules/islandora_audio/tests/src/Functional/GenerateAudioDerivativeTest.php @@ -11,6 +11,9 @@ use Drupal\Tests\islandora\Functional\GenerateDerivativeTestBase; */ class GenerateAudioDerivativeTest extends GenerateDerivativeTestBase { + /** + * {@inheritdoc} + */ protected static $modules = ['context_ui', 'islandora_audio']; /** diff --git a/modules/islandora_breadcrumbs/tests/src/Functional/BreadcrumbsTest.php b/modules/islandora_breadcrumbs/tests/src/Functional/BreadcrumbsTest.php index 4ba101fe..c95d362c 100644 --- a/modules/islandora_breadcrumbs/tests/src/Functional/BreadcrumbsTest.php +++ b/modules/islandora_breadcrumbs/tests/src/Functional/BreadcrumbsTest.php @@ -24,12 +24,32 @@ class BreadcrumbsTest extends IslandoraFunctionalTestBase { ]; + /** + * A node. + * + * @var \Drupal\node\NodeInterface + */ protected $nodeA; + /** + * Another node. + * + * @var \Drupal\node\NodeInterface + */ protected $nodeB; + /** + * Yet another node. + * + * @var \Drupal\node\NodeInterface + */ protected $nodeC; + /** + * Another one. + * + * @var \Drupal\node\NodeInterface + */ protected $nodeD; /** diff --git a/modules/islandora_image/tests/src/Functional/GenerateImageDerivativeTest.php b/modules/islandora_image/tests/src/Functional/GenerateImageDerivativeTest.php index 69e18ba7..c917ba6c 100644 --- a/modules/islandora_image/tests/src/Functional/GenerateImageDerivativeTest.php +++ b/modules/islandora_image/tests/src/Functional/GenerateImageDerivativeTest.php @@ -11,6 +11,9 @@ use Drupal\Tests\islandora\Functional\GenerateDerivativeTestBase; */ class GenerateImageDerivativeTest extends GenerateDerivativeTestBase { + /** + * {@inheritdoc} + */ protected static $modules = ['context_ui', 'islandora_image']; /** diff --git a/modules/islandora_video/tests/src/Functional/GenerateVideoDerivativeTest.php b/modules/islandora_video/tests/src/Functional/GenerateVideoDerivativeTest.php index 64bd6d4a..02beca06 100644 --- a/modules/islandora_video/tests/src/Functional/GenerateVideoDerivativeTest.php +++ b/modules/islandora_video/tests/src/Functional/GenerateVideoDerivativeTest.php @@ -11,6 +11,9 @@ use Drupal\Tests\islandora\Functional\GenerateDerivativeTestBase; */ class GenerateVideoDerivativeTest extends GenerateDerivativeTestBase { + /** + * {@inheritdoc} + */ protected static $modules = ['context_ui', 'islandora_video']; /** diff --git a/src/ContextReaction/NormalizerAlterReaction.php b/src/ContextReaction/NormalizerAlterReaction.php index 503c7248..1dcd5c68 100644 --- a/src/ContextReaction/NormalizerAlterReaction.php +++ b/src/ContextReaction/NormalizerAlterReaction.php @@ -7,6 +7,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\jsonld\Form\JsonLdSettingsForm; +use Drupal\islandora\IslandoraUtils; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -25,16 +26,25 @@ abstract class NormalizerAlterReaction extends ContextReactionPluginBase impleme */ protected $jsonldConfig; + /** + * Islandora utils. + * + * @var \Drupal\islandora\IslandoraUtils + */ + protected $utils; + /** * {@inheritdoc} */ public function __construct(array $configuration, $plugin_id, $plugin_definition, - ConfigFactoryInterface $config_factory) { + ConfigFactoryInterface $config_factory, + IslandoraUtils $utils) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->jsonldConfig = $config_factory->get(JsonLdSettingsForm::CONFIG_NAME); + $this->utils = $utils; } /** @@ -45,7 +55,8 @@ abstract class NormalizerAlterReaction extends ContextReactionPluginBase impleme $configuration, $plugin_id, $plugin_definition, - $container->get('config.factory') + $container->get('config.factory'), + $container->get('islandora.utils') ); } @@ -71,11 +82,11 @@ abstract class NormalizerAlterReaction extends ContextReactionPluginBase impleme * The url. */ protected function getSubjectUrl(EntityInterface $entity) { - $url = $entity->toUrl('canonical', ['absolute' => TRUE]); + $format = ''; if (!$this->jsonldConfig->get(JsonLdSettingsForm::REMOVE_JSONLD_FORMAT)) { - $url->setRouteParameter('_format', 'jsonld'); + $format = 'jsonld'; } - return $url->toString(); + return $this->utils->getRestUrl($entity, $format); } } diff --git a/src/Controller/MediaSourceController.php b/src/Controller/MediaSourceController.php index 28377ddc..2e5b4b26 100644 --- a/src/Controller/MediaSourceController.php +++ b/src/Controller/MediaSourceController.php @@ -11,6 +11,7 @@ use Drupal\media\MediaInterface; use Drupal\media\MediaTypeInterface; use Drupal\node\NodeInterface; use Drupal\taxonomy\TermInterface; +use Drupal\islandora\IslandoraUtils; use Drupal\islandora\MediaSource\MediaSourceService; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; @@ -39,6 +40,13 @@ class MediaSourceController extends ControllerBase { */ protected $database; + /** + * Islandora utils. + * + * @var \Drupal\islandora\IslandoraUtils + */ + protected $utils; + /** * MediaSourceController constructor. * @@ -46,13 +54,17 @@ class MediaSourceController extends ControllerBase { * Service for business logic. * @param \Drupal\Core\Database\Connection $database * Database connection. + * @param \Drupal\islandora\IslandoraUtils $utils + * Islandora utils. */ public function __construct( MediaSourceService $service, - Connection $database + Connection $database, + IslandoraUtils $utils ) { $this->service = $service; $this->database = $database; + $this->utils = $utils; } /** @@ -67,7 +79,8 @@ class MediaSourceController extends ControllerBase { public static function create(ContainerInterface $container) { return new static( $container->get('islandora.media_source_service'), - $container->get('database') + $container->get('database'), + $container->get('islandora.utils') ); } @@ -162,7 +175,7 @@ class MediaSourceController extends ControllerBase { // We return the media if it was newly created. if ($media) { $response = new Response("", 201); - $response->headers->set("Location", $media->url('canonical', ['absolute' => TRUE])); + $response->headers->set("Location", $this->utils->getEntityUrl($media)); } else { $response = new Response("", 204); diff --git a/src/EventGenerator/EventGenerator.php b/src/EventGenerator/EventGenerator.php index a9e4ac66..bf95e97d 100644 --- a/src/EventGenerator/EventGenerator.php +++ b/src/EventGenerator/EventGenerator.php @@ -3,8 +3,7 @@ namespace Drupal\islandora\EventGenerator; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\Core\Url; +use Drupal\islandora\IslandoraUtils; use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\user\UserInterface; @@ -16,11 +15,11 @@ use Drupal\user\UserInterface; class EventGenerator implements EventGeneratorInterface { /** - * Language manager. + * Islandora utils. * - * @var \Drupal\Core\Language\LanguageManagerInterface + * @var \Drupal\islandora\IslandoraUtils */ - protected $languageManager; + protected $utils; /** * Media source service. @@ -32,13 +31,13 @@ class EventGenerator implements EventGeneratorInterface { /** * Constructor. * - * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager - * Language manager. + * @param \Drupal\islandora\IslandoraUtils $utils + * Islandora utils. * @param \Drupal\islandora\MediaSource\MediaSourceService $media_source * Media source service. */ - public function __construct(LanguageManagerInterface $language_manager, MediaSourceService $media_source) { - $this->languageManager = $language_manager; + public function __construct(IslandoraUtils $utils, MediaSourceService $media_source) { + $this->utils = $utils; $this->mediaSource = $media_source; } @@ -47,21 +46,16 @@ class EventGenerator implements EventGeneratorInterface { */ public function generateEvent(EntityInterface $entity, UserInterface $user, array $data) { - $user_url = $user->toUrl()->setAbsolute()->toString(); - $entity_type = $entity->getEntityTypeId(); + $user_url = $this->utils->getEntityUrl($user); - $undefined = $this->languageManager->getLanguage('und'); + $entity_type = $entity->getEntityTypeId(); if ($entity_type == 'file') { - $entity_url = $entity->url('canonical', ['absolute' => TRUE, 'language' => $undefined]); + $entity_url = $this->utils->getDownloadUrl($entity); $mimetype = $entity->getMimeType(); } else { - $entity_url = Url::fromRoute( - "rest.entity.$entity_type.GET", - [$entity_type => $entity->id()], - ['absolute' => TRUE, 'language' => $undefined] - )->toString(); + $entity_url = $this->utils->getEntityUrl($entity); $mimetype = 'text/html'; } @@ -110,14 +104,14 @@ class EventGenerator implements EventGeneratorInterface { $event['object']['url'][] = [ "name" => "JSON", "type" => "Link", - "href" => "$entity_url?_format=json", + "href" => $this->utils->getRestUrl($entity, 'json'), "mediaType" => "application/json", "rel" => "alternate", ]; $event['object']['url'][] = [ "name" => "JSONLD", "type" => "Link", - "href" => "$entity_url?_format=jsonld", + "href" => $this->utils->getRestUrl($entity, 'jsonld'), "mediaType" => "application/ld+json", "rel" => "alternate", ]; @@ -130,7 +124,7 @@ class EventGenerator implements EventGeneratorInterface { $event['object']['url'][] = [ "name" => "Describes", "type" => "Link", - "href" => $file->url('canonical', ['absolute' => TRUE, 'language' => $undefined]), + "href" => $this->utils->getDownloadUrl($file), "mediaType" => $file->getMimeType(), "rel" => "describes", ]; diff --git a/src/EventSubscriber/LinkHeaderSubscriber.php b/src/EventSubscriber/LinkHeaderSubscriber.php index 6bb184fd..4edf7da4 100644 --- a/src/EventSubscriber/LinkHeaderSubscriber.php +++ b/src/EventSubscriber/LinkHeaderSubscriber.php @@ -6,10 +6,9 @@ use Drupal\Core\Access\AccessManagerInterface; use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManager; -use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Url; +use Drupal\islandora\IslandoraUtils; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; @@ -66,11 +65,11 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { protected $requestStack; /** - * Language manager. + * Islandora utils. * - * @var \Drupal\Core\Language\LanguageManagerInterface + * @var \Drupal\islandora\IslandoraUtils */ - protected $languageManager; + protected $utils; /** * Constructor. @@ -87,8 +86,8 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { * The route match object. * @param Symfony\Component\HttpFoundation\RequestStack $request_stack * Request stack (for current request). - * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager - * Language manager. + * @param \Drupal\islandora\IslandoraUtils $utils + * Islandora utils. */ public function __construct( EntityTypeManager $entity_type_manager, @@ -97,7 +96,7 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { AccountInterface $account, RouteMatchInterface $route_match, RequestStack $request_stack, - LanguageManagerInterface $language_manager + IslandoraUtils $utils ) { $this->entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; @@ -107,7 +106,7 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { $this->accessManager = $access_manager; $this->account = $account; $this->requestStack = $request_stack; - $this->languageManager = $language_manager; + $this->utils = $utils; } /** @@ -206,6 +205,8 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { // Headers are subject to an access check. if ($referencedEntity->access('view')) { + $entity_url = $this->utils->getEntityUrl($referencedEntity); + // Taxonomy terms are written out as // ; rel="tag"; title="Tag Name" // where url is defined in field_same_as. @@ -213,7 +214,6 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { // it becomes the taxonomy term's local uri. if ($referencedEntity->getEntityTypeId() == 'taxonomy_term') { $rel = "tag"; - $entity_url = $referencedEntity->url('canonical', ['absolute' => TRUE]); if ($referencedEntity->hasField('field_external_uri')) { $external_uri = $referencedEntity->get('field_external_uri')->getValue(); if (!empty($external_uri) && isset($external_uri[0]['uri'])) { @@ -228,7 +228,6 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { // ; rel="related"; title="Field Label" // and the url is the local uri. $rel = "related"; - $entity_url = $referencedEntity->url('canonical', ['absolute' => TRUE]); $title = $field_definition->label(); } $links[] = "<$entity_url>; rel=\"$rel\"; title=\"$title\""; @@ -287,19 +286,16 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface { continue; } + // Skip route if the user doesn't have access. $meta_route_name = "rest.entity.$entity_type.GET"; - $route_params = [$entity_type => $entity->id()]; - if (!$this->accessManager->checkNamedRoute($meta_route_name, $route_params, $this->account)) { continue; } - $meta_url = Url::fromRoute($meta_route_name, $route_params) - ->setAbsolute() - ->toString(); + $meta_url = $this->utils->getRestUrl($entity, $format); - $links[] = "<$meta_url?_format=$format>; rel=\"alternate\"; type=\"$mime\""; + $links[] = "<$meta_url>; rel=\"alternate\"; type=\"$mime\""; } } diff --git a/src/EventSubscriber/MediaLinkHeaderSubscriber.php b/src/EventSubscriber/MediaLinkHeaderSubscriber.php index 6e344f0d..644db872 100644 --- a/src/EventSubscriber/MediaLinkHeaderSubscriber.php +++ b/src/EventSubscriber/MediaLinkHeaderSubscriber.php @@ -77,10 +77,9 @@ class MediaLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSub } // Collect file links for the media. - $undefined = $this->languageManager->getLanguage('und'); foreach ($media->get($source_field)->referencedEntities() as $referencedEntity) { if ($referencedEntity->access('view')) { - $file_url = $referencedEntity->url('canonical', ['absolute' => TRUE, 'language' => $undefined]); + $file_url = $this->utils->getDownloadUrl($referencedEntity); $links[] = "<$file_url>; rel=\"describes\"; type=\"{$referencedEntity->getMimeType()}\""; } } diff --git a/src/EventSubscriber/NodeLinkHeaderSubscriber.php b/src/EventSubscriber/NodeLinkHeaderSubscriber.php index 4d898f8f..443ce86f 100644 --- a/src/EventSubscriber/NodeLinkHeaderSubscriber.php +++ b/src/EventSubscriber/NodeLinkHeaderSubscriber.php @@ -2,17 +2,8 @@ namespace Drupal\islandora\EventSubscriber; -use Drupal\Core\Access\AccessManagerInterface; -use Drupal\Core\Entity\EntityFieldManager; -use Drupal\Core\Entity\EntityTypeManager; -use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\Core\Session\AccountInterface; -use Drupal\Core\Url; -use Drupal\islandora\IslandoraUtils; use Drupal\node\NodeInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; -use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** @@ -22,55 +13,6 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; */ class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubscriberInterface { - /** - * Derivative utils. - * - * @var \Drupal\islandora\IslandoraUtils - */ - protected $utils; - - /** - * Constructor. - * - * @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager - * The entity type manager. - * @param \Drupal\Core\Entity\EntityFieldManager $entity_field_manager - * The entity field manager. - * @param \Drupal\Core\Access\AccessManagerInterface $access_manager - * The access manager. - * @param \Drupal\Core\Session\AccountInterface $account - * The current user. - * @param \Drupal\Core\Routing\RouteMatchInterface $route_match - * The route match object. - * @param Symfony\Component\HttpFoundation\RequestStack $request_stack - * Request stack (for current request). - * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager - * Language manager. - * @param \Drupal\islandora\IslandoraUtils $utils - * Derivative utils. - */ - public function __construct( - EntityTypeManager $entity_type_manager, - EntityFieldManager $entity_field_manager, - AccessManagerInterface $access_manager, - AccountInterface $account, - RouteMatchInterface $route_match, - RequestStack $request_stack, - LanguageManagerInterface $language_manager, - IslandoraUtils $utils - ) { - parent::__construct( - $entity_type_manager, - $entity_field_manager, - $access_manager, - $account, - $route_match, - $request_stack, - $language_manager - ); - $this->utils = $utils; - } - /** * Adds node-specific link headers to appropriate responses. * @@ -105,13 +47,8 @@ class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubs */ protected function generateRelatedMediaLinks(NodeInterface $node) { $links = []; - $undefined = $this->languageManager->getLanguage('und'); foreach ($this->utils->getMedia($node) as $media) { - $url = Url::fromRoute( - "rest.entity.media.GET", - ['media' => $media->id()], - ['absolute' => TRUE, 'language' => $undefined] - )->toString(); + $url = $this->utils->getEntityUrl($media); foreach ($media->referencedEntities() as $term) { if ($term->getEntityTypeId() == 'taxonomy_term' && $term->hasField('field_external_uri')) { $field = $term->get('field_external_uri'); diff --git a/src/Flysystem/Adapter/FedoraAdapter.php b/src/Flysystem/Adapter/FedoraAdapter.php index 03900753..4aebde6e 100644 --- a/src/Flysystem/Adapter/FedoraAdapter.php +++ b/src/Flysystem/Adapter/FedoraAdapter.php @@ -20,7 +20,18 @@ class FedoraAdapter implements AdapterInterface { use StreamedCopyTrait; use NotSupportingVisibilityTrait; + /** + * Fedora client. + * + * @var \Islandora\Chullo\IFedoraApi + */ protected $fedora; + + /** + * Mimetype guesser. + * + * @var \Symfony\Component\HttpFoundation\File\Mimetype\MimeTypeGuesserInterface + */ protected $mimeTypeGuesser; /** @@ -75,7 +86,6 @@ class FedoraAdapter implements AdapterInterface { $meta = $this->getMetadataFromHeaders($response); $meta['path'] = $path; - if ($meta['type'] == 'file') { $meta['stream'] = StreamWrapper::getResource($response->getBody()); } diff --git a/src/Flysystem/Fedora.php b/src/Flysystem/Fedora.php index 85f42b1a..b90fc529 100644 --- a/src/Flysystem/Fedora.php +++ b/src/Flysystem/Fedora.php @@ -27,8 +27,18 @@ class Fedora implements FlysystemPluginInterface, ContainerFactoryPluginInterfac use FlysystemUrlTrait; + /** + * Fedora client. + * + * @var \Islandora\Chullo\IFedoraApi + */ protected $fedora; + /** + * Mimetype guesser. + * + * @var \Symfony\Component\HttpFoundation\File\Mimetype\MimeTypeGuesserInterface + */ protected $mimeTypeGuesser; /** diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php index e6ba1b75..e98766c7 100644 --- a/src/IslandoraUtils.php +++ b/src/IslandoraUtils.php @@ -4,12 +4,15 @@ namespace Drupal\islandora; use Drupal\context\ContextManager; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\Query\QueryException; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Site\Settings; +use Drupal\Core\Url; use Drupal\file\FileInterface; use Drupal\flysystem\FlysystemFactory; use Drupal\islandora\ContextProvider\NodeContextProvider; @@ -64,6 +67,13 @@ class IslandoraUtils { */ protected $flysystemFactory; + /** + * Language manager. + * + * @var \Drupal\Core\Language\LanguageManagerInterface + */ + protected $languageManager; + /** * Constructor. * @@ -77,19 +87,23 @@ class IslandoraUtils { * Context manager. * @param \Drupal\flysystem\FlysystemFactory $flysystem_factory * Flysystem factory. + * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * Language manager. */ public function __construct( EntityTypeManager $entity_type_manager, EntityFieldManager $entity_field_manager, QueryFactory $entity_query, ContextManager $context_manager, - FlysystemFactory $flysystem_factory + FlysystemFactory $flysystem_factory, + LanguageManagerInterface $language_manager ) { $this->entityTypeManager = $entity_type_manager; $this->entityFieldManager = $entity_field_manager; $this->entityQuery = $entity_query; $this->contextManager = $context_manager; $this->flysystemFactory = $flysystem_factory; + $this->languageManager = $language_manager; } /** @@ -498,4 +512,62 @@ class IslandoraUtils { return $condition; } + /** + * Gets the id URL of an entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity whose URL you want. + * + * @return string + * The entity URL. + */ + public function getEntityUrl(EntityInterface $entity) { + $undefined = $this->languageManager->getLanguage('und'); + $entity_type = $entity->getEntityTypeId(); + return Url::fromRoute( + "entity.$entity_type.canonical", + [$entity_type => $entity->id()], + ['absolute' => TRUE, 'language' => $undefined] + )->toString(); + } + + /** + * Gets the downloadable URL for a file. + * + * @param \Drupal\file\FileInterface $file + * The file whose URL you want. + * + * @return string + * The file URL. + */ + public function getDownloadUrl(FileInterface $file) { + $undefined = $this->languageManager->getLanguage('und'); + return $file->url('canonical', ['absolute' => TRUE, 'language' => $undefined]); + } + + /** + * Gets the URL for an entity's REST endpoint. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity whose REST endpoint you want. + * @param string $format + * REST serialization format. + * + * @return string + * The REST URL. + */ + public function getRestUrl(EntityInterface $entity, $format = '') { + $undefined = $this->languageManager->getLanguage('und'); + $entity_type = $entity->getEntityTypeId(); + $rest_url = Url::fromRoute( + "rest.entity.$entity_type.GET", + [$entity_type => $entity->id()], + ['absolute' => TRUE, 'language' => $undefined] + )->toString(); + if (!empty($format)) { + $rest_url .= "?_format=$format"; + } + return $rest_url; + } + } diff --git a/src/Plugin/Action/AbstractGenerateDerivative.php b/src/Plugin/Action/AbstractGenerateDerivative.php index 4635a95c..233ce774 100644 --- a/src/Plugin/Action/AbstractGenerateDerivative.php +++ b/src/Plugin/Action/AbstractGenerateDerivative.php @@ -155,7 +155,7 @@ class AbstractGenerateDerivative extends EmitEvent { throw new \RuntimeException("Could not locate source file for media {$source_media->id()}", 500); } - $data['source_uri'] = $source_file->url('canonical', ['absolute' => TRUE]); + $data['source_uri'] = $this->utils->getDownloadUrl($source_file); // Find the term for the derivative and use it to set the destination url // in the data array. diff --git a/src/Plugin/ContextReaction/MappingUriPredicateReaction.php b/src/Plugin/ContextReaction/MappingUriPredicateReaction.php index c59e48c8..05f2f8c0 100644 --- a/src/Plugin/ContextReaction/MappingUriPredicateReaction.php +++ b/src/Plugin/ContextReaction/MappingUriPredicateReaction.php @@ -9,6 +9,7 @@ use Drupal\islandora\ContextReaction\NormalizerAlterReaction; use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\jsonld\Normalizer\NormalizerBase; use Drupal\media\MediaInterface; +use Drupal\islandora\IslandoraUtils; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -23,6 +24,11 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction { const URI_PREDICATE = 'drupal_uri_predicate'; + /** + * Media source service. + * + * @var \Drupal\islandora\MediaSource\MediaSourceService + */ protected $mediaSource; /** @@ -32,13 +38,15 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction { $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, + IslandoraUtils $utils, MediaSourceService $media_source) { parent::__construct( $configuration, $plugin_id, $plugin_definition, - $config_factory + $config_factory, + $utils ); $this->mediaSource = $media_source; } @@ -52,6 +60,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction { $plugin_id, $plugin_definition, $container->get('config.factory'), + $container->get('islandora.utils'), $container->get('islandora.media_source_service') ); } @@ -80,7 +89,7 @@ class MappingUriPredicateReaction extends NormalizerAlterReaction { // Swap media and file urls. if ($entity instanceof MediaInterface) { $file = $this->mediaSource->getSourceFile($entity); - $graph['@id'] = $file->url('canonical', ['absolute' => TRUE]); + $graph['@id'] = $this->utils->getDownloadUrl($file); } if (isset($graph[$drupal_predicate])) { if (!is_array($graph[$drupal_predicate])) { diff --git a/src/PresetReaction/PresetReaction.php b/src/PresetReaction/PresetReaction.php index 0584b997..98aa6946 100644 --- a/src/PresetReaction/PresetReaction.php +++ b/src/PresetReaction/PresetReaction.php @@ -14,6 +14,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; */ class PresetReaction extends ContextReactionPluginBase implements ContainerFactoryPluginInterface { + /** + * Action storage. + * + * @var \Drupal\Core\Entity\EntityStorageInterface + */ protected $actionStorage; /** diff --git a/tests/src/Functional/GenerateDerivativeTestBase.php b/tests/src/Functional/GenerateDerivativeTestBase.php index b650ca3b..c74ff255 100644 --- a/tests/src/Functional/GenerateDerivativeTestBase.php +++ b/tests/src/Functional/GenerateDerivativeTestBase.php @@ -7,6 +7,9 @@ namespace Drupal\Tests\islandora\Functional; */ abstract class GenerateDerivativeTestBase extends IslandoraFunctionalTestBase { + /** + * {@inheritdoc} + */ protected static $modules = ['context_ui']; /** diff --git a/tests/src/Functional/IslandoraFunctionalTestBase.php b/tests/src/Functional/IslandoraFunctionalTestBase.php index e1d6e91f..9a50fb05 100644 --- a/tests/src/Functional/IslandoraFunctionalTestBase.php +++ b/tests/src/Functional/IslandoraFunctionalTestBase.php @@ -20,8 +20,14 @@ class IslandoraFunctionalTestBase extends BrowserTestBase { use TestFileCreationTrait; use MediaTypeCreationTrait; + /** + * {@inheritdoc} + */ protected static $modules = ['context_ui', 'field_ui', 'islandora']; + /** + * {@inheritdoc} + */ protected static $configSchemaCheckerExclusions = [ 'jwt.config', 'context.context.test', @@ -29,12 +35,28 @@ class IslandoraFunctionalTestBase extends BrowserTestBase { 'context.context.media', 'context.context.file', 'key.key.test', + 'media.settings', ]; + /** + * Test node type. + * + * @var \Drupal\node\Entity\NodeType + */ protected $testType; + /** + * Test media type. + * + * @var \Drupal\media\Entity\MediaType + */ protected $testMediaType; + /** + * Test vocabulary. + * + * @var \Drupal\taxonomy\Entity\Vocabulary + */ protected $testVocabulary; /** @@ -170,6 +192,10 @@ EOD; $destination->write($name, $source->read($name)); } + $media_settings = $this->container->get('config.factory')->getEditable('media.settings'); + $media_settings->set('standalone_url', TRUE); + $media_settings->save(TRUE); + // Cache clear / rebuild. drupal_flush_all_caches(); $this->container->get('router.builder')->rebuild(); diff --git a/tests/src/Kernel/EventGeneratorTest.php b/tests/src/Kernel/EventGeneratorTest.php index 21491be0..c423cda3 100644 --- a/tests/src/Kernel/EventGeneratorTest.php +++ b/tests/src/Kernel/EventGeneratorTest.php @@ -65,7 +65,7 @@ class EventGeneratorTest extends IslandoraKernelTestBase { // Create the event generator so we can test it. $this->eventGenerator = new EventGenerator( - $this->container->get('language_manager'), + $this->container->get('islandora.utils'), $this->container->get('islandora.media_source_service') ); } diff --git a/tests/src/Kernel/GeminiClientFactoryTest.php b/tests/src/Kernel/GeminiClientFactoryTest.php index 3259c221..1bbd8e75 100644 --- a/tests/src/Kernel/GeminiClientFactoryTest.php +++ b/tests/src/Kernel/GeminiClientFactoryTest.php @@ -18,6 +18,11 @@ use Psr\Log\LoggerInterface; */ class GeminiClientFactoryTest extends IslandoraKernelTestBase { + /** + * Logger. + * + * @var \Psr\Log\LoggerInterface + */ private $logger; /** diff --git a/tests/src/Kernel/GeminiLookupTest.php b/tests/src/Kernel/GeminiLookupTest.php index 600fea73..15edc2e8 100644 --- a/tests/src/Kernel/GeminiLookupTest.php +++ b/tests/src/Kernel/GeminiLookupTest.php @@ -23,18 +23,53 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; */ class GeminiLookupTest extends IslandoraKernelTestBase { + /** + * JWT Auth. + * + * @var \Drupal\jwt\Authentication\Provider\JwtAuth + */ private $jwtAuth; + /** + * Logger. + * + * @var \Psr\Log\LoggerInterface + */ private $logger; + /** + * Guzzle. + * + * @var \GuzzleHttp\Client + */ private $guzzle; + /** + * Gemini client. + * + * @var \Islandora\Crayfish\Commons\Client\GeminiClient + */ private $geminiClient; + /** + * Media source service. + * + * @var \Drupal\islandora\MediaSource\MediaSourceService + */ private $mediaSource; + /** + * An entity. + * + * @var \Drupal\Core\Entity\EntityInterface + */ private $entity; + /** + * A media. + * + * @var \Drupal\media\MediaInterface + */ private $media; /**