Browse Source

Adding describes url to messages and enforcing language neutrality in a few more places.

pull/729/head 1.0.0
dannylamb 6 years ago
parent
commit
28c74495c8
  1. 6
      islandora.services.yml
  2. 32
      src/EventGenerator/EventGenerator.php
  3. 14
      src/EventSubscriber/LinkHeaderSubscriber.php
  4. 3
      src/EventSubscriber/MediaLinkHeaderSubscriber.php
  5. 29
      src/EventSubscriber/NodeLinkHeaderSubscriber.php
  6. 27
      tests/src/Kernel/EventGeneratorTest.php

6
islandora.services.yml

@ -3,7 +3,7 @@
services: services:
islandora.eventgenerator: islandora.eventgenerator:
class: Drupal\islandora\EventGenerator\EventGenerator class: Drupal\islandora\EventGenerator\EventGenerator
arguments: ['@language_manager'] arguments: ['@language_manager', '@islandora.media_source_service']
islandora.stomp: islandora.stomp:
class: Stomp\StatefulStomp class: Stomp\StatefulStomp
factory: ['Drupal\islandora\StompFactory', create] factory: ['Drupal\islandora\StompFactory', create]
@ -16,12 +16,12 @@ services:
- { name: event_subscriber } - { name: event_subscriber }
islandora.media_link_header_subscriber: islandora.media_link_header_subscriber:
class: Drupal\islandora\EventSubscriber\MediaLinkHeaderSubscriber class: Drupal\islandora\EventSubscriber\MediaLinkHeaderSubscriber
arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack'] arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@language_manager']
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
islandora.node_link_header_subscriber: islandora.node_link_header_subscriber:
class: Drupal\islandora\EventSubscriber\NodeLinkHeaderSubscriber class: Drupal\islandora\EventSubscriber\NodeLinkHeaderSubscriber
arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@islandora.utils'] arguments: ['@entity_type.manager', '@entity_field.manager', '@access_manager', '@current_user', '@current_route_match', '@request_stack', '@language_manager', '@islandora.utils']
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
islandora.admin_view_route_subscriber: islandora.admin_view_route_subscriber:

32
src/EventGenerator/EventGenerator.php

@ -5,6 +5,7 @@ namespace Drupal\islandora\EventGenerator;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\islandora\MediaSource\MediaSourceService;
use Drupal\user\UserInterface; use Drupal\user\UserInterface;
/** /**
@ -21,14 +22,24 @@ class EventGenerator implements EventGeneratorInterface {
*/ */
protected $languageManager; protected $languageManager;
/**
* Media source service.
*
* @var \Drupal\islandora\MediaSource\MediaSourceService
*/
protected $mediaSource;
/** /**
* Constructor. * Constructor.
* *
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager. * Language manager.
* @param \Drupal\islandora\MediaSource\MediaSourceService $media_source
* Media source service.
*/ */
public function __construct(LanguageManagerInterface $language_manager) { public function __construct(LanguageManagerInterface $language_manager, MediaSourceService $media_source) {
$this->languageManager = $language_manager; $this->languageManager = $language_manager;
$this->mediaSource = $media_source;
} }
/** /**
@ -39,12 +50,13 @@ class EventGenerator implements EventGeneratorInterface {
$user_url = $user->toUrl()->setAbsolute()->toString(); $user_url = $user->toUrl()->setAbsolute()->toString();
$entity_type = $entity->getEntityTypeId(); $entity_type = $entity->getEntityTypeId();
$undefined = $this->languageManager->getLanguage('und');
if ($entity_type == 'file') { if ($entity_type == 'file') {
$entity_url = $entity->url(); $entity_url = $entity->url('canonical', ['absolute' => TRUE, 'language' => $undefined]);
$mimetype = $entity->getMimeType(); $mimetype = $entity->getMimeType();
} }
else { else {
$undefined = $this->languageManager->getLanguage('und');
$entity_url = Url::fromRoute( $entity_url = Url::fromRoute(
"rest.entity.$entity_type.GET", "rest.entity.$entity_type.GET",
[$entity_type => $entity->id()], [$entity_type => $entity->id()],
@ -111,6 +123,20 @@ class EventGenerator implements EventGeneratorInterface {
]; ];
} }
// Add a link to the file described by a media.
if ($entity_type == 'media') {
$file = $this->mediaSource->getSourceFile($entity);
if ($file) {
$event['object']['url'][] = [
"name" => "Describes",
"type" => "Link",
"href" => $file->url('canonical', ['absolute' => TRUE, 'language' => $undefined]),
"mediaType" => $file->getMimeType(),
"rel" => "describes",
];
}
}
unset($data["event"]); unset($data["event"]);
unset($data["queue"]); unset($data["queue"]);

14
src/EventSubscriber/LinkHeaderSubscriber.php

@ -6,6 +6,7 @@ use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
@ -64,6 +65,13 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
*/ */
protected $requestStack; protected $requestStack;
/**
* Language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/** /**
* Constructor. * Constructor.
* *
@ -79,6 +87,8 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
* The route match object. * The route match object.
* @param Symfony\Component\HttpFoundation\RequestStack $request_stack * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
* Request stack (for current request). * Request stack (for current request).
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager.
*/ */
public function __construct( public function __construct(
EntityTypeManager $entity_type_manager, EntityTypeManager $entity_type_manager,
@ -86,7 +96,8 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
AccessManagerInterface $access_manager, AccessManagerInterface $access_manager,
AccountInterface $account, AccountInterface $account,
RouteMatchInterface $route_match, RouteMatchInterface $route_match,
RequestStack $request_stack RequestStack $request_stack,
LanguageManagerInterface $language_manager
) { ) {
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager; $this->entityFieldManager = $entity_field_manager;
@ -96,6 +107,7 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
$this->accessManager = $access_manager; $this->accessManager = $access_manager;
$this->account = $account; $this->account = $account;
$this->requestStack = $request_stack; $this->requestStack = $request_stack;
$this->languageManager = $language_manager;
} }
/** /**

3
src/EventSubscriber/MediaLinkHeaderSubscriber.php

@ -77,9 +77,10 @@ class MediaLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSub
} }
// Collect file links for the media. // Collect file links for the media.
$undefined = $this->languageManager->getLanguage('und');
foreach ($media->get($source_field)->referencedEntities() as $referencedEntity) { foreach ($media->get($source_field)->referencedEntities() as $referencedEntity) {
if ($referencedEntity->access('view')) { if ($referencedEntity->access('view')) {
$file_url = $referencedEntity->url('canonical', ['absolute' => TRUE]); $file_url = $referencedEntity->url('canonical', ['absolute' => TRUE, 'language' => $undefined]);
$links[] = "<$file_url>; rel=\"describes\"; type=\"{$referencedEntity->getMimeType()}\""; $links[] = "<$file_url>; rel=\"describes\"; type=\"{$referencedEntity->getMimeType()}\"";
} }
} }

29
src/EventSubscriber/NodeLinkHeaderSubscriber.php

@ -5,8 +5,10 @@ namespace Drupal\islandora\EventSubscriber;
use Drupal\Core\Access\AccessManagerInterface; use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\islandora\IslandoraUtils; use Drupal\islandora\IslandoraUtils;
use Drupal\node\NodeInterface; use Drupal\node\NodeInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
@ -42,6 +44,8 @@ class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubs
* The route match object. * The route match object.
* @param Symfony\Component\HttpFoundation\RequestStack $request_stack * @param Symfony\Component\HttpFoundation\RequestStack $request_stack
* Request stack (for current request). * Request stack (for current request).
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager.
* @param \Drupal\islandora\IslandoraUtils $utils * @param \Drupal\islandora\IslandoraUtils $utils
* Derivative utils. * Derivative utils.
*/ */
@ -52,16 +56,18 @@ class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubs
AccountInterface $account, AccountInterface $account,
RouteMatchInterface $route_match, RouteMatchInterface $route_match,
RequestStack $request_stack, RequestStack $request_stack,
LanguageManagerInterface $language_manager,
IslandoraUtils $utils IslandoraUtils $utils
) { ) {
$this->entityTypeManager = $entity_type_manager; parent::__construct(
$this->entityFieldManager = $entity_field_manager; $entity_type_manager,
$this->accessManager = $access_manager; $entity_field_manager,
$this->account = $account; $access_manager,
$this->routeMatch = $route_match; $account,
$this->accessManager = $access_manager; $route_match,
$this->account = $account; $request_stack,
$this->requestStack = $request_stack; $language_manager
);
$this->utils = $utils; $this->utils = $utils;
} }
@ -99,8 +105,13 @@ class NodeLinkHeaderSubscriber extends LinkHeaderSubscriber implements EventSubs
*/ */
protected function generateRelatedMediaLinks(NodeInterface $node) { protected function generateRelatedMediaLinks(NodeInterface $node) {
$links = []; $links = [];
$undefined = $this->languageManager->getLanguage('und');
foreach ($this->utils->getMedia($node) as $media) { foreach ($this->utils->getMedia($node) as $media) {
$url = $media->url('canonical', ['absolute' => TRUE]); $url = Url::fromRoute(
"rest.entity.media.GET",
['media' => $media->id()],
['absolute' => TRUE, 'language' => $undefined]
)->toString();
foreach ($media->referencedEntities() as $term) { foreach ($media->referencedEntities() as $term) {
if ($term->getEntityTypeId() == 'taxonomy_term' && $term->hasField('field_external_uri')) { if ($term->getEntityTypeId() == 'taxonomy_term' && $term->hasField('field_external_uri')) {
$field = $term->get('field_external_uri'); $field = $term->get('field_external_uri');

27
tests/src/Kernel/EventGeneratorTest.php

@ -64,7 +64,10 @@ class EventGeneratorTest extends IslandoraKernelTestBase {
$this->entity->save(); $this->entity->save();
// Create the event generator so we can test it. // Create the event generator so we can test it.
$this->eventGenerator = new EventGenerator($this->container->get('language_manager')); $this->eventGenerator = new EventGenerator(
$this->container->get('language_manager'),
$this->container->get('islandora.media_source_service')
);
} }
/** /**
@ -146,9 +149,12 @@ class EventGeneratorTest extends IslandoraKernelTestBase {
foreach ($msg['actor']['url'] as $url) { foreach ($msg['actor']['url'] as $url) {
$this->assertTrue($url['type'] == 'Link', "'url' entries must have type 'Link'"); $this->assertTrue($url['type'] == 'Link', "'url' entries must have type 'Link'");
$this->assertTrue( $this->assertTrue(
$url['mediaType'] == 'application/ld+json' || $url['mediaType'] == 'text/html', in_array(
"'url' entries must be either html or jsonld" $url['mediaType'],
); ['application/json', 'application/ld+json', 'text/html']
),
"'url' entries must be either html, json, or jsonld"
);
} }
// Make sure the object exists and is a uri. // Make sure the object exists and is a uri.
@ -158,13 +164,16 @@ class EventGeneratorTest extends IslandoraKernelTestBase {
$msg["object"]["id"] == "urn:uuid:{$this->entity->uuid()}", $msg["object"]["id"] == "urn:uuid:{$this->entity->uuid()}",
"Id must be an URN with entity's UUID" "Id must be an URN with entity's UUID"
); );
$this->assertTrue(array_key_exists("url", $msg["actor"]), "Object must have 'url' key."); $this->assertTrue(array_key_exists("url", $msg["object"]), "Object must have 'url' key.");
foreach ($msg['actor']['url'] as $url) { foreach ($msg['object']['url'] as $url) {
$this->assertTrue($url['type'] == 'Link', "'url' entries must have type 'Link'"); $this->assertTrue($url['type'] == 'Link', "'url' entries must have type 'Link'");
$this->assertTrue( $this->assertTrue(
$url['mediaType'] == 'application/ld+json' || $url['mediaType'] == 'text/html', in_array(
"'url' entries must be either html or jsonld" $url['mediaType'],
); ['application/json', 'application/ld+json', 'text/html']
),
"'url' entries must be either html, json, or jsonld"
);
} }
} }

Loading…
Cancel
Save