Skip to content

Commit

Permalink
Merge pull request #869 from Islandora/iiif_title
Browse files Browse the repository at this point in the history
Set IIIF Manifest title from source entity
  • Loading branch information
jordandukart authored May 20, 2022
2 parents 0322808 + e5a1f99 commit 93c19b6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
12 changes: 2 additions & 10 deletions config/schema/islandora.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ condition.plugin.node_had_namespace:
pid_field:
type: ignore
label: 'PID field'

field.formatter.settings.islandora_image:
type: mapping
label: 'Image field display format settings'
mapping:
image_link:
type: string
label: 'Link image to'
image_style:
type: string
label: 'Image style'
type: field.formatter.settings.image
label: 'Islandora image field display format settings'
50 changes: 46 additions & 4 deletions modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Drupal\islandora_iiif\Plugin\views\style;

use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -68,6 +70,13 @@ class IIIFManifest extends StylePluginBase {
*/
protected $iiifConfig;

/**
* The Drupal Entity Type Manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* The Drupal Filesystem.
*
Expand All @@ -85,12 +94,13 @@ class IIIFManifest extends StylePluginBase {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config, FileSystemInterface $file_system, Client $http_client, MessengerInterface $messenger) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, SerializerInterface $serializer, Request $request, ImmutableConfig $iiif_config, EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system, Client $http_client, MessengerInterface $messenger) {
parent::__construct($configuration, $plugin_id, $plugin_definition);

$this->serializer = $serializer;
$this->request = $request;
$this->iiifConfig = $iiif_config;
$this->entityTypeManager = $entity_type_manager;
$this->fileSystem = $file_system;
$this->httpClient = $http_client;
$this->messenger = $messenger;
Expand All @@ -107,6 +117,7 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('serializer'),
$container->get('request_stack')->getCurrentRequest(),
$container->get('config.factory')->get('islandora_iiif.settings'),
$container->get('entity_type.manager'),
$container->get('file_system'),
$container->get('http_client'),
$container->get('messenger')
Expand All @@ -121,18 +132,21 @@ public function render() {
$iiif_address = $this->iiifConfig->get('iiif_server');
if (!is_null($iiif_address) && !empty($iiif_address)) {
// Get the current URL being requested.
$request_url = $this->request->getSchemeAndHttpHost() . $this->request->getRequestUri();
$request_host = $this->request->getSchemeAndHttpHost();
$request_url = $this->request->getRequestUri();
// Strip off the last URI component to get the base ID of the URL.
// @todo assumming the view is a path like /node/1/manifest.json
$url_components = explode('/', $request_url);
array_pop($url_components);
$iiif_base_id = implode('/', $url_components);
$content_path = implode('/', $url_components);
$iiif_base_id = $request_host . '/' . $content_path;

// @see https://iiif.io/api/presentation/2.1/#manifest
$json += [
'@type' => 'sc:Manifest',
'@id' => $request_url,
// If the View has a title, set the View title as the manifest label.
'label' => $this->view->getTitle() ?: 'IIIF Manifest',
'label' => $this->view->getTitle() ?: $this->getEntityTitle($content_path),
'@context' => 'http://iiif.io/api/presentation/2/context.json',
// @see https://iiif.io/api/presentation/2.1/#sequence
'sequences' => [
Expand Down Expand Up @@ -260,6 +274,34 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas
return $canvases;
}

/**
* Pull a title from the node or media passed to this view.
*
* @param string $content_path
* The path of the content being requested.
*
* @return string
* The entity's title.
*/
public function getEntityTitle(string $content_path): string {
$entity_title = $this->t('IIIF Manifest');
try {
$params = Url::fromUserInput($content_path)->getRouteParameters();
if (isset($params['node'])) {
$node = $this->entityTypeManager->getStorage('node')->load($params['node']);
$entity_title = $node->getTitle();
}
elseif (isset($params['media'])) {
$media = $this->entityTypeManager->getStorage('media')->load($params['media']);
$entity_title = $media->getName();
}
}
catch (\InvalidArgumentException $e) {

}
return $entity_title;
}

/**
* {@inheritdoc}
*/
Expand Down
11 changes: 8 additions & 3 deletions tests/src/Functional/IslandoraImageFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ public function testIslandoraImageFormatter() {
$testImageMediaType = $this->createMediaType('image', ['id' => 'test_image_media_type']);
$testImageMediaType->save();
$this->createEntityReferenceField('media', $testImageMediaType->id(), 'field_media_of', 'Media Of', 'node', 'default', [], 2);

// Set the display mode to use the islandora_image formatter.
// Also, only show the image on display to remove clutter.
$display_options = [
'type' => 'islandora_image',
'settings' => ['image_style' => NULL, 'image_link' => 'content'],
'settings' => [
'image_style' => '',
'image_link' => 'content',
'image_loading' => [
'attribute' => 'eager',
],
],
];

$display = $this->container->get('entity_display.repository')->getViewDisplay('media', $testImageMediaType->id(), 'default');
$display->setComponent('field_media_image', $display_options)
->removeComponent('created')
Expand All @@ -47,7 +53,6 @@ public function testIslandoraImageFormatter() {
'title' => 'Test Node',
]);
$node->save();

// Make a image for the Media.
$file = $this->container->get('entity_type.manager')->getStorage('file')->create([
'uid' => $account->id(),
Expand Down

0 comments on commit 93c19b6

Please sign in to comment.