From 6804242bb4db5aa864f6f053f3d1e58275f3b805 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 30 May 2023 12:10:30 -0300 Subject: [PATCH 1/5] Make the mirador config more easily alterable, Drupal-side. --- islandora_mirador.libraries.yml | 1 + islandora_mirador.module | 32 +++++++++-- js/mirador_viewer.js | 57 +++++-------------- src/Plugin/Block/MiradorBlock.php | 10 +--- .../FieldFormatter/MiradorImageFormatter.php | 8 +-- 5 files changed, 47 insertions(+), 61 deletions(-) diff --git a/islandora_mirador.libraries.yml b/islandora_mirador.libraries.yml index 9ecdd30..9d0c595 100644 --- a/islandora_mirador.libraries.yml +++ b/islandora_mirador.libraries.yml @@ -15,6 +15,7 @@ viewer: theme: css/mirador.css: {} dependencies: + - core/once - core/drupal - core/drupalSettings - islandora_mirador/mirador diff --git a/islandora_mirador.module b/islandora_mirador.module index 92df821..5d4621e 100644 --- a/islandora_mirador.module +++ b/islandora_mirador.module @@ -27,11 +27,10 @@ function islandora_mirador_theme() { } /** - * Implements template_preprocess_*(). + * Preprocess variables for mirador templates. */ function template_preprocess_mirador(&$variables) { $variables['mirador_view_id'] = Html::getUniqueId($variables['mirador_view_id']); - $variables['#attached']['drupalSettings']['mirador_view_id'] = $variables['mirador_view_id']; /** * @var \Drupal\islandora_mirador\IslandoraMiradorPluginManager @@ -41,11 +40,11 @@ function template_preprocess_mirador(&$variables) { /** * @var \Drupal\Core\Config\ImmutableConfig */ - $config = Drupal::service('config.factory')->get('islandora_mirador.settings'); + $config = \Drupal::service('config.factory')->get('islandora_mirador.settings'); $mirador_plugins = $mirador_plugin_manager->getDefinitions(); $enabled_plugins = $config->get('mirador_enabled_plugins'); - $variables['#attached']['drupalSettings']['mirador_enabled_plugins'] = array_filter(array_values($enabled_plugins)); + $variables['#attached']['drupalSettings']['mirador']['enabled_plugins'] = array_filter(array_values($enabled_plugins)); $window_config = []; foreach ($mirador_plugins as $plugin_id => $plugin_definition) { @@ -57,7 +56,32 @@ function template_preprocess_mirador(&$variables) { $plugin_instance->windowConfigAlter($window_config); } } + + // XXX: Maintain the original properties, for now. + $variables['#attached']['drupalSettings']['mirador_view_id'] = $variables['mirador_view_id']; $variables['#attached']['drupalSettings']['mirador_window_settings'] = $window_config; + $variables['#attached']['drupalSettings']['mirador_enabled_plugins'] = $variables['#attached']['drupalSettings']['mirador']['enabled_plugins']; + + // mirador.window_settings ... isn't presently used on its own? + $variables['#attached']['drupalSettings']['mirador']['window_settings'] = $window_config; + + // mirador.viewers is an associative array mapping CSS/jQuery/once selectors + // to objects of mirador configuration. + $variables['#attached']['drupalSettings']['mirador']['viewers']["#{$variables['mirador_view_id']}"] = [ + 'id' => $variables['mirador_view_id'], + 'manifests' => [ + $variables['iiif_manifest_url'] => [ + 'provider' => 'Islandora' + ], + ], + 'window' => $window_config, + 'windows' => [ + [ + 'manifestId' => $variables['iiif_manifest_url'], + 'thumbnailNavigationPosition' => 'far-bottom', + ], + ] + ]; } /** diff --git a/js/mirador_viewer.js b/js/mirador_viewer.js index 3f45fa9..7e65ef7 100644 --- a/js/mirador_viewer.js +++ b/js/mirador_viewer.js @@ -1,57 +1,30 @@ -/*jslint browser: true*/ -/*global Mirador, textOverlayPlugin, Drupal*/ +/*jslint browser: true, esversion: 6 */ +/*global Mirador, Drupal, once*/ /** * @file * Displays Mirador viewer. */ -(function ($, Drupal) { +(function (Drupal, once) { 'use strict'; - /** - * If initialized. - * @type {boolean} - */ - var initialized; - /** - * Unique HTML id. - * @type {string} - */ - var base; - - function init(context,settings){ - if (!initialized){ - initialized = true; - - /* - Mirador - */ - var miradorInstance = Mirador.viewer({ - "id": base, - "manifests": { - [settings.iiif_manifest_url]: {provider: "Islandora"} - }, - "window": settings.mirador_window_settings, - "windows": [ - { - "manifestId": settings.iiif_manifest_url, - "thumbnailNavigationPosition": 'far-bottom' - } - ] - }, window.miradorPlugins); - } - } - Drupal.Mirador = Drupal.Mirador || {}; - /** * Initialize the Mirador Viewer. */ Drupal.behaviors.Mirador = { attach: function (context, settings) { - base = settings.mirador_view_id; - init(context,settings); + Object.entries(settings.mirador.viewers).forEach(entry => { + const [base, values] = entry; + once('mirador-viewer', base, context).forEach(() => + Mirador.viewer(values) + ); + }); }, - detach: function () { + detach: function (context, settings) { + Object.entries(settings.mirador.viewers).forEach(entry => { + const [base, ] = entry; + once.remove('mirador-viewer', base, context); + }); } }; -})(jQuery, Drupal); +})(Drupal, once); diff --git a/src/Plugin/Block/MiradorBlock.php b/src/Plugin/Block/MiradorBlock.php index a3c2c55..15ce125 100644 --- a/src/Plugin/Block/MiradorBlock.php +++ b/src/Plugin/Block/MiradorBlock.php @@ -110,17 +110,11 @@ public function build() { $manifest_url = $this->token->replace($this->configuration['iiif_manifest_url'], ['node' => $node]); $id = 'mirador_' . $node->id(); $build = [ - "#title" => $this->t('Mirador Viewer'), - "#description" => $this->t("A div for mirador viewer"), + //"#title" => $this->t('Mirador Viewer'), + //"#description" => $this->t("A div for mirador viewer"), "#theme" => "mirador", '#mirador_view_id' => $id, '#iiif_manifest_url' => $manifest_url, - "#attached" => [ - 'drupalSettings' => [ - 'iiif_manifest_url' => $manifest_url, - 'mirador_view_id' => $id, - ], - ], ]; return $build; diff --git a/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php b/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php index 93034a8..7edeb92 100644 --- a/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php +++ b/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php @@ -127,16 +127,10 @@ public function viewElements(FieldItemListInterface $items, $langcode) { $node = $first_media->get('field_media_of')->entity; $id = 'mirador_' . $node->id(); $manifest_url = $token_service->replace($iiif_url, ['node' => $node]); - $elements[] = [ + $elements[$id] = [ '#theme' => 'mirador', '#mirador_view_id' => $id, '#iiif_manifest_url' => $manifest_url, - '#attached' => [ - 'drupalSettings' => [ - 'iiif_manifest_url' => $manifest_url, - 'mirador_view_id' => $id, - ], - ], '#settings' => $settings, ]; } From 1b1c31298422e2d0bb3a97cd30432653b498c73f Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 30 May 2023 12:26:49 -0300 Subject: [PATCH 2/5] Add the block title/description back, and wrap up the themed element. --- src/Plugin/Block/MiradorBlock.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Plugin/Block/MiradorBlock.php b/src/Plugin/Block/MiradorBlock.php index 15ce125..6af44b8 100644 --- a/src/Plugin/Block/MiradorBlock.php +++ b/src/Plugin/Block/MiradorBlock.php @@ -110,11 +110,13 @@ public function build() { $manifest_url = $this->token->replace($this->configuration['iiif_manifest_url'], ['node' => $node]); $id = 'mirador_' . $node->id(); $build = [ - //"#title" => $this->t('Mirador Viewer'), - //"#description" => $this->t("A div for mirador viewer"), - "#theme" => "mirador", - '#mirador_view_id' => $id, - '#iiif_manifest_url' => $manifest_url, + "#title" => $this->t('Mirador Viewer'), + "#description" => $this->t("A div for mirador viewer"), + 'viewer' => [ + "#theme" => "mirador", + '#mirador_view_id' => $id, + '#iiif_manifest_url' => $manifest_url, + ], ]; return $build; From 9d12ad1c07f40bd6b093f55adaf27094e68512c7 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 30 May 2023 14:33:17 -0300 Subject: [PATCH 3/5] Add reference to the plugins back. --- js/mirador_viewer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/mirador_viewer.js b/js/mirador_viewer.js index 7e65ef7..80b1dba 100644 --- a/js/mirador_viewer.js +++ b/js/mirador_viewer.js @@ -15,7 +15,7 @@ Object.entries(settings.mirador.viewers).forEach(entry => { const [base, values] = entry; once('mirador-viewer', base, context).forEach(() => - Mirador.viewer(values) + Mirador.viewer(values, window.miradorPlugins) ); }); }, From 86daf664ebf43926d70833d96680c529360bf4a5 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Tue, 30 May 2023 15:01:19 -0300 Subject: [PATCH 4/5] Be more permissive, for if the plugins are not available. --- js/mirador_viewer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/mirador_viewer.js b/js/mirador_viewer.js index 80b1dba..2d30fc4 100644 --- a/js/mirador_viewer.js +++ b/js/mirador_viewer.js @@ -15,7 +15,7 @@ Object.entries(settings.mirador.viewers).forEach(entry => { const [base, values] = entry; once('mirador-viewer', base, context).forEach(() => - Mirador.viewer(values, window.miradorPlugins) + Mirador.viewer(values, window.miradorPlugins || {}) ); }); }, From 7013307b633755bdd8a995532da4e8ded3563884 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Mon, 19 Jun 2023 15:18:28 -0300 Subject: [PATCH 5/5] Add cache metadata, and fix array offsets... ... apparently, is strict about the keys being numeric. --- .../Field/FieldFormatter/MiradorImageFormatter.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php b/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php index 7edeb92..3f1250a 100644 --- a/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php +++ b/src/Plugin/Field/FieldFormatter/MiradorImageFormatter.php @@ -2,6 +2,7 @@ namespace Drupal\islandora_mirador\Plugin\Field\FieldFormatter; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; @@ -119,21 +120,29 @@ public function viewElements(FieldItemListInterface $items, $langcode) { if (empty($files)) { return $elements; } + $iiif_url = $this->configFactory->get('islandora_mirador.settings')->get('iiif_manifest_url'); $token_service = $this->token; foreach ($files as $file) { + $cache_meta = CacheableMetadata::createFromObject($file); + $medias = $this->utils->getReferencingMedia($file->id()); $first_media = array_values($medias)[0]; + $cache_meta->addCacheableDependency($first_media); $node = $first_media->get('field_media_of')->entity; + $cache_meta->addCacheableDependency($node); $id = 'mirador_' . $node->id(); $manifest_url = $token_service->replace($iiif_url, ['node' => $node]); - $elements[$id] = [ + $renderable = [ '#theme' => 'mirador', '#mirador_view_id' => $id, '#iiif_manifest_url' => $manifest_url, '#settings' => $settings, ]; + $cache_meta->applyTo($renderable); + $elements[] = $renderable; } + return $elements; }