From 4d1a95d1da489847dffcc5a2abe1a329926f5f7c Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Thu, 7 Mar 2024 16:59:15 -0500 Subject: [PATCH 01/13] Add hOCR functionality --- .../config/schema/islandora_iiif.schema.yml | 12 ++ .../src/Plugin/views/style/IIIFManifest.php | 105 ++++++++++++++++-- 2 files changed, 105 insertions(+), 12 deletions(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index 1f91450fa..c3dcb794a 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -19,3 +19,15 @@ views.style.iiif_manifest: type: sequence sequence: type: string + label: "Tile source field(s)" + iiif_ocr_file_field: + type: sequence + sequence: + type: string: + label: "IIIF hOCR file field" + structured_text_term: + type: string + label: "Structured text term" + search_endpoint: + type: string + label: "Search endpoint path" diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 2fc8a431a..fc3b159c9 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -11,6 +11,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; +use Drupal\islandora\IslandoraUtils; use Drupal\views\Plugin\views\style\StylePluginBase; use Drupal\views\ResultRow; use GuzzleHttp\Client; @@ -35,6 +36,13 @@ */ class IIIFManifest extends StylePluginBase { + /** + * Islandora utility functions. + * + * @var \Drupal\islandora\IslandoraUtils + */ + protected $utils; + /** * {@inheritdoc} */ @@ -111,7 +119,7 @@ class IIIFManifest extends StylePluginBase { /** * {@inheritdoc} */ - 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, ModuleHandlerInterface $moduleHandler) { + 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, ModuleHandlerInterface $moduleHandler, IslandoraUtils $utils) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->serializer = $serializer; @@ -121,6 +129,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->fileSystem = $file_system; $this->httpClient = $http_client; $this->messenger = $messenger; + $this->utils = $utils; $this->moduleHandler = $moduleHandler; } @@ -139,7 +148,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('file_system'), $container->get('http_client'), $container->get('messenger'), - $container->get('module_handler') + $container->get('module_handler'), + $container->get('islandora.utils') ); } @@ -188,6 +198,11 @@ public function render() { $label = $this->t("IIIF Manifest"); } + /** + * @var \Drupal\taxonomy\TermInterface|null + */ + $structured_text_term = $this->utils->getTermForUri($this->options['structured_text_term_uri']); + // @see https://iiif.io/api/presentation/2.1/#manifest $json += [ '@type' => 'sc:Manifest', @@ -207,7 +222,7 @@ public function render() { // For each row in the View result. foreach ($this->view->result as $row) { // Add the IIIF URL to the image to print out as JSON. - $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id); + $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id, $structured_text_term); foreach ($canvases as $tile_source) { $json['sequences'][0]['canvases'][] = $tile_source; } @@ -217,6 +232,9 @@ public function render() { $content_type = 'json'; + // Add a search endpoint if one is defined + $this->addSearchEndpoint($json, $url_components); + // Give other modules a chance to alter the manifest. $this->moduleHandler->alter('islandora_iiif_manifest', $json, $this); @@ -237,7 +255,7 @@ public function render() { * @return array * List of IIIF URLs to display in the Openseadragon viewer. */ - protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id) { + protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id, $structured_text_term) { $canvases = []; foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) { $viewsField = $this->view->field[$iiif_tile_field]; @@ -300,7 +318,7 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas ], ]; - if ($ocr_url = $this->getOcrUrl($entity, $row, $i)) { + if ($ocr_url = $this->getOcrUrl($entity, $structured_text_term)) { $tmp_canvas['seeAlso'] = [ '@id' => $ocr_url, 'format' => 'text/vnd.hocr+html', @@ -389,19 +407,29 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima * The absolute URL of the current row's structured text, * or FALSE if none. */ - protected function getOcrUrl(EntityInterface $entity, ResultRow $row, $delta) { + protected function getOcrUrl(EntityInterface $entity, $structured_text_term) { $ocr_url = FALSE; $iiif_ocr_file_field = !empty($this->options['iiif_ocr_file_field']) ? array_filter(array_values($this->options['iiif_ocr_file_field'])) : []; $ocrField = count($iiif_ocr_file_field) > 0 ? $this->view->field[$iiif_ocr_file_field[0]] : NULL; if ($ocrField) { - $ocr_entity = $ocrField->getEntity($row); + $ocr_entity = $entity; $ocr_field_name = $ocrField->definition['field_name']; if (!is_null($ocr_field_name)) { $ocrs = $ocr_entity->{$ocr_field_name}; - $ocr = isset($ocrs[$delta]) ? $ocrs[$delta] : FALSE; - if ($ocr) { - $ocr_url = $ocr->entity->createFileUrl(FALSE); - } + $ocr = $ocrs[0] ?? FALSE; + $ocr_url = $ocr->entity->createFileUrl(FALSE); + } + } + elseif ($structured_text_term) { + $parent_node = $this->utils->getParentNode($entity); + $ocr_entity_array = $this->utils->getMediaReferencingNodeAndTerm($parent_node, $structured_text_term); + $ocr_entity_id = is_array($ocr_entity_array) ? array_shift($ocr_entity_array) : NULL; + $ocr_entity = $ocr_entity_id ? $this->entityTypeManager->getStorage('media')->load($ocr_entity_id) : NULL; + if ($ocr_entity) { + $ocr_file_source = $ocr_entity->getSource(); + $ocr_fid = $ocr_file_source->getSourceFieldValue($ocr_entity); + $ocr_file = $this->entityTypeManager->getStorage('file')->load($ocr_fid); + $ocr_url = $ocr_file->createFileUrl(FALSE); } } @@ -448,6 +476,21 @@ protected function defineOptions() { return $options; } + protected function addSearchEndpoint(array &$json, array $url_components) { + $url_base = $this->getRequest()->getSchemeAndHttpHost(); + $hocr_search_path = $this->options['search_endpoint']; + $hocr_search_url = $url_base . '/' . ltrim($hocr_search_path, '/'); + + $hocr_search_url = str_replace('%node', $url_components[1], $hocr_search_url); + + $json['service'][] = [ + "@context" => "http://iiif.io/api/search/0/context.json", + "@id" => $hocr_search_url, + "profile" => "http://iiif.io/api/search/0/search", + "label" => t("Search inside this work"), + ]; + } + /** * {@inheritdoc} */ @@ -504,10 +547,27 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#title' => $this->t('Structured OCR data file field'), '#type' => 'checkboxes', '#default_value' => $this->options['iiif_ocr_file_field'], - '#description' => $this->t('The source of structured OCR text for each entity.'), + '#description' => $this->t('The source of structured OCR text for each entity. If the term setting below is left blank, it will be the same entity as the source image'), '#options' => $field_options, '#required' => FALSE, ]; + + $form['structured_text_term'] = [ + '#type' => 'entity_autocomplete', + '#target_type' => 'taxonomy_term', + '#title' => $this->t('Structured OCR text term'), + '#default_value' => $this->utils->getTermForUri($this->options['structured_text_term_uri']), + '#required' => FALSE, + '#description' => $this->t('Term indicating the media that holds structured text, such as hOCR, for the given object. Use this if the text is on a separate media from the tile source.'), + ]; + + $form['search_endpoint'] = [ + '#type' => 'textfield', + '#title' => $this->t("Search endpoint path."), + '#description' => $this->t("If there is a search endpoint to search within the book that returns IIIF annotations, put it here. Use substitutions %node and %keywords.
E.g., paged-content-search/%node?search-in-pages=%keywords"), + '#default_value' => $this->options['search_endpoint'], + '#required' => FALSE, + ]; } /** @@ -520,4 +580,25 @@ public function getFormats() { return ['json' => 'json']; } + /** + * Submit handler for options form. + * + * Used to store the structured text media term by URL instead of Ttid. + * + * @param array $form + * The form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state object. + */ + // @codingStandardsIgnoreStart + public function submitOptionsForm(&$form, FormStateInterface $form_state) { + // @codingStandardsIgnoreEnd + $style_options = $form_state->getValue('style_options'); + $tid = $style_options['structured_text_term']; + $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); + $style_options['structured_text_term_uri'] = $this->utils->getUriForTerm($term); + $form_state->setValue('style_options', $style_options); + parent::submitOptionsForm($form, $form_state); + } + } From 821bb61bb75334bce566b74e9b261e1f153fc7c7 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 13 Mar 2024 11:43:11 -0300 Subject: [PATCH 02/13] Fix wording in Islandora IIIF View Style config form. --- .../islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index fc3b159c9..cef91f9ea 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -547,7 +547,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#title' => $this->t('Structured OCR data file field'), '#type' => 'checkboxes', '#default_value' => $this->options['iiif_ocr_file_field'], - '#description' => $this->t('The source of structured OCR text for each entity. If the term setting below is left blank, it will be the same entity as the source image'), + '#description' => $this->t("If the hOCR is a field on the same entity as the image source field above, select it here. If it's found in a related entity via the term below, leave this blank."), '#options' => $field_options, '#required' => FALSE, ]; @@ -564,7 +564,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $form['search_endpoint'] = [ '#type' => 'textfield', '#title' => $this->t("Search endpoint path."), - '#description' => $this->t("If there is a search endpoint to search within the book that returns IIIF annotations, put it here. Use substitutions %node and %keywords.
E.g., paged-content-search/%node?search-in-pages=%keywords"), + '#description' => $this->t("If there is a search endpoint to search within the book that returns IIIF annotations, put it here. Use %node substitution where needed.
E.g., paged-content-search/%node"), '#default_value' => $this->options['search_endpoint'], '#required' => FALSE, ]; From 863e85a76091740de44cca8159318eb96f440e99 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sun, 17 Mar 2024 21:50:40 -0300 Subject: [PATCH 03/13] Islandora IIIF: Attempt to fix broken config schema. --- .../islandora_iiif/config/schema/islandora_iiif.schema.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index c3dcb794a..c98187240 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -19,12 +19,12 @@ views.style.iiif_manifest: type: sequence sequence: type: string - label: "Tile source field(s)" + label: "Tile source field(s)" iiif_ocr_file_field: type: sequence sequence: type: string: - label: "IIIF hOCR file field" + label: "IIIF hOCR file field" structured_text_term: type: string label: "Structured text term" From 174c0bd62a494d2a8ec3ceccc3addfea514bd2aa Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sun, 17 Mar 2024 22:21:21 -0300 Subject: [PATCH 04/13] Islandora IIIF: Remove labels from config schema. --- modules/islandora_iiif/config/schema/islandora_iiif.schema.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index c98187240..93578080a 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -19,12 +19,10 @@ views.style.iiif_manifest: type: sequence sequence: type: string - label: "Tile source field(s)" iiif_ocr_file_field: type: sequence sequence: type: string: - label: "IIIF hOCR file field" structured_text_term: type: string label: "Structured text term" From 68bdbcf4fd361a8365c3732d1ace1e81a3d7300e Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sun, 17 Mar 2024 22:37:21 -0300 Subject: [PATCH 05/13] Islandora IIIF: Add labels to config schema items. --- .../islandora_iiif/config/schema/islandora_iiif.schema.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index 93578080a..d7e8b8040 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -17,12 +17,14 @@ views.style.iiif_manifest: mapping: iiif_tile_field: type: sequence + label: "Tile source field(s)" sequence: type: string iiif_ocr_file_field: type: sequence + label: "Structured OCR data file field" sequence: - type: string: + type: string structured_text_term: type: string label: "Structured text term" From 0ae6b4022c84ed383afbaa14838af2e1a73f7a19 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sun, 17 Mar 2024 23:06:29 -0300 Subject: [PATCH 06/13] Islandora IIIF: Address PHPCS errors. --- .../src/Plugin/views/style/IIIFManifest.php | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index cef91f9ea..05462a036 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -232,7 +232,7 @@ public function render() { $content_type = 'json'; - // Add a search endpoint if one is defined + // Add a search endpoint if one is defined. $this->addSearchEndpoint($json, $url_components); // Give other modules a chance to alter the manifest. @@ -251,6 +251,8 @@ public function render() { * @param string $iiif_base_id * The URL for the request, minus the last part of the URL, * which is likely "manifest". + * @param \Drupal\taxonomy\TermInterface $structured_text_term + * The term representing the media use. * * @return array * List of IIIF URLs to display in the Openseadragon viewer. @@ -398,10 +400,8 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity at the current row. - * @param \Drupal\views\ResultRow $row - * Result row. - * @param int $delta - * The delta in case there are multiple canvases on one media. + * @param \Drupal\taxonomy\TermInterface $structured_text_term + * The term representing the media use. * * @return string|false * The absolute URL of the current row's structured text, @@ -476,6 +476,14 @@ protected function defineOptions() { return $options; } + /** + * Undocumented function + * + * @param array $json + * The IIIF manifest. + * @param array $url_components + * The search endpoint URL as array. + */ protected function addSearchEndpoint(array &$json, array $url_components) { $url_base = $this->getRequest()->getSchemeAndHttpHost(); $hocr_search_path = $this->options['search_endpoint']; @@ -484,10 +492,10 @@ protected function addSearchEndpoint(array &$json, array $url_components) { $hocr_search_url = str_replace('%node', $url_components[1], $hocr_search_url); $json['service'][] = [ - "@context" => "http://iiif.io/api/search/0/context.json", - "@id" => $hocr_search_url, - "profile" => "http://iiif.io/api/search/0/search", - "label" => t("Search inside this work"), + "@context" => "http://iiif.io/api/search/0/context.json", + "@id" => $hocr_search_url, + "profile" => "http://iiif.io/api/search/0/search", + "label" => t("Search inside this work"), ]; } From 56166a69a3ae090d966d7ae5dc02a2f5287f945b Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Sun, 17 Mar 2024 23:21:04 -0300 Subject: [PATCH 07/13] Islandora IIIF: Address PHPCS errors. --- .../islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 05462a036..30425c29b 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -12,6 +12,7 @@ use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Url; use Drupal\islandora\IslandoraUtils; +use Drupal\taxonomy\TermInterface; use Drupal\views\Plugin\views\style\StylePluginBase; use Drupal\views\ResultRow; use GuzzleHttp\Client; @@ -251,7 +252,7 @@ public function render() { * @param string $iiif_base_id * The URL for the request, minus the last part of the URL, * which is likely "manifest". - * @param \Drupal\taxonomy\TermInterface $structured_text_term + * @param $structured_text_term * The term representing the media use. * * @return array @@ -400,7 +401,7 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity at the current row. - * @param \Drupal\taxonomy\TermInterface $structured_text_term + * @param $structured_text_term * The term representing the media use. * * @return string|false @@ -477,7 +478,7 @@ protected function defineOptions() { } /** - * Undocumented function + * Add the configured search endpoint to the manifest. * * @param array $json * The IIIF manifest. From 8b52d4d856587fd3e1bf7f44c700d3220e9c9ecd Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 18 Mar 2024 00:16:55 -0300 Subject: [PATCH 08/13] Islandora IIIF: Address PHPCS errors. --- .../islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 30425c29b..407c8741f 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -401,14 +401,14 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity at the current row. - * @param $structured_text_term + * @param \Drupal\taxonomy\TermInterface $structured_text_term * The term representing the media use. * * @return string|false * The absolute URL of the current row's structured text, * or FALSE if none. */ - protected function getOcrUrl(EntityInterface $entity, $structured_text_term) { + protected function getOcrUrl(EntityInterface $entity, TermInterface $structured_text_term) { $ocr_url = FALSE; $iiif_ocr_file_field = !empty($this->options['iiif_ocr_file_field']) ? array_filter(array_values($this->options['iiif_ocr_file_field'])) : []; $ocrField = count($iiif_ocr_file_field) > 0 ? $this->view->field[$iiif_ocr_file_field[0]] : NULL; From c40ab45b9975455b74d74e9e09510cb65afde867 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Mon, 18 Mar 2024 19:43:05 -0300 Subject: [PATCH 09/13] Update IIIFManifest.php Co-authored-by: Rosie Le Faive --- modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 407c8741f..3560d6cb0 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -252,7 +252,7 @@ public function render() { * @param string $iiif_base_id * The URL for the request, minus the last part of the URL, * which is likely "manifest". - * @param $structured_text_term + * @param \Drupal\taxonomy\TermInterface $structured_text_term * The term representing the media use. * * @return array From ca2fa86aba7bc88d39f2d6fe1e2ff0d5c77b5595 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 20 Mar 2024 04:40:26 -0300 Subject: [PATCH 10/13] Islandorqa IIIF: Address PHPCS errors. --- modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 3560d6cb0..6d1e65348 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -258,7 +258,7 @@ public function render() { * @return array * List of IIIF URLs to display in the Openseadragon viewer. */ - protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id, $structured_text_term) { + protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id, TermInterface $structured_text_term) { $canvases = []; foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) { $viewsField = $this->view->field[$iiif_tile_field]; From 71a11eeefc3ef7c0992b0a8703b90f577e8ee1f8 Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 20 Mar 2024 14:34:28 -0300 Subject: [PATCH 11/13] Move to memoized term lookup, instead of polluting method call. --- .../src/Plugin/views/style/IIIFManifest.php | 48 +++++++++++++------ 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 6d1e65348..03d3f6bb2 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -117,6 +117,20 @@ class IIIFManifest extends StylePluginBase { */ protected $moduleHandler; + /** + * Memoized structured text term. + * + * @var \Drupal\taxonomy\TermInterface|null + */ + protected ?TermInterface $structuredTextTerm; + + /** + * Flag to track if we _have_ attempted a lookup, as the value is nullable. + * + * @var bool + */ + protected bool $structuredTextTermMemoized = FALSE; + /** * {@inheritdoc} */ @@ -199,11 +213,6 @@ public function render() { $label = $this->t("IIIF Manifest"); } - /** - * @var \Drupal\taxonomy\TermInterface|null - */ - $structured_text_term = $this->utils->getTermForUri($this->options['structured_text_term_uri']); - // @see https://iiif.io/api/presentation/2.1/#manifest $json += [ '@type' => 'sc:Manifest', @@ -223,7 +232,7 @@ public function render() { // For each row in the View result. foreach ($this->view->result as $row) { // Add the IIIF URL to the image to print out as JSON. - $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id, $structured_text_term); + $canvases = $this->getTileSourceFromRow($row, $iiif_address, $iiif_base_id); foreach ($canvases as $tile_source) { $json['sequences'][0]['canvases'][] = $tile_source; } @@ -252,13 +261,11 @@ public function render() { * @param string $iiif_base_id * The URL for the request, minus the last part of the URL, * which is likely "manifest". - * @param \Drupal\taxonomy\TermInterface $structured_text_term - * The term representing the media use. * * @return array * List of IIIF URLs to display in the Openseadragon viewer. */ - protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id, TermInterface $structured_text_term) { + protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_base_id) { $canvases = []; foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) { $viewsField = $this->view->field[$iiif_tile_field]; @@ -321,7 +328,7 @@ protected function getTileSourceFromRow(ResultRow $row, $iiif_address, $iiif_bas ], ]; - if ($ocr_url = $this->getOcrUrl($entity, $structured_text_term)) { + if ($ocr_url = $this->getOcrUrl($entity)) { $tmp_canvas['seeAlso'] = [ '@id' => $ocr_url, 'format' => 'text/vnd.hocr+html', @@ -401,14 +408,12 @@ protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $ima * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity at the current row. - * @param \Drupal\taxonomy\TermInterface $structured_text_term - * The term representing the media use. * * @return string|false * The absolute URL of the current row's structured text, * or FALSE if none. */ - protected function getOcrUrl(EntityInterface $entity, TermInterface $structured_text_term) { + protected function getOcrUrl(EntityInterface $entity) { $ocr_url = FALSE; $iiif_ocr_file_field = !empty($this->options['iiif_ocr_file_field']) ? array_filter(array_values($this->options['iiif_ocr_file_field'])) : []; $ocrField = count($iiif_ocr_file_field) > 0 ? $this->view->field[$iiif_ocr_file_field[0]] : NULL; @@ -421,7 +426,7 @@ protected function getOcrUrl(EntityInterface $entity, TermInterface $structured_ $ocr_url = $ocr->entity->createFileUrl(FALSE); } } - elseif ($structured_text_term) { + elseif ($structured_text_term = $this->getStructuredTextTerm()) { $parent_node = $this->utils->getParentNode($entity); $ocr_entity_array = $this->utils->getMediaReferencingNodeAndTerm($parent_node, $structured_text_term); $ocr_entity_id = is_array($ocr_entity_array) ? array_shift($ocr_entity_array) : NULL; @@ -610,4 +615,19 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) { parent::submitOptionsForm($form, $form_state); } + /** + * Get the structured text term. + * + * @return \Drupal\taxonomy\TermInterface|null + * The term if it could be found; otherwise, NULL. + */ + protected function getStructuredTextTerm() : ?TermInterface { + if (!$this->structuredTextTermMemoized) { + $this->structuredTextTermMemoized = TRUE; + $this->structuredTextTerm = $this->utils->getTermForUri($this->options['structured_text_term_uri']); + } + + return $this->structuredTextTerm; + } + } From f896c4f5358abe880e39b5403c42b3bad8d3021e Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 20 Mar 2024 14:39:29 -0300 Subject: [PATCH 12/13] Deal explicitly with URIs, instead of attempting to put the entity in config. --- modules/islandora_iiif/config/schema/islandora_iiif.schema.yml | 2 +- modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml index d7e8b8040..11fff4c71 100644 --- a/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml +++ b/modules/islandora_iiif/config/schema/islandora_iiif.schema.yml @@ -25,7 +25,7 @@ views.style.iiif_manifest: label: "Structured OCR data file field" sequence: type: string - structured_text_term: + structured_text_term_uri: type: string label: "Structured text term" search_endpoint: diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index 03d3f6bb2..ebf5a9225 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -570,7 +570,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { '#type' => 'entity_autocomplete', '#target_type' => 'taxonomy_term', '#title' => $this->t('Structured OCR text term'), - '#default_value' => $this->utils->getTermForUri($this->options['structured_text_term_uri']), + '#default_value' => $this->getStructuredTextTerm(), '#required' => FALSE, '#description' => $this->t('Term indicating the media that holds structured text, such as hOCR, for the given object. Use this if the text is on a separate media from the tile source.'), ]; @@ -609,6 +609,7 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) { // @codingStandardsIgnoreEnd $style_options = $form_state->getValue('style_options'); $tid = $style_options['structured_text_term']; + unset($style_options['structured_text_term']); $term = $this->entityTypeManager->getStorage('taxonomy_term')->load($tid); $style_options['structured_text_term_uri'] = $this->utils->getUriForTerm($term); $form_state->setValue('style_options', $style_options); From 92721aa70fd42c45f6b1b341bffeb210d50faac1 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Wed, 17 Apr 2024 13:49:22 -0300 Subject: [PATCH 13/13] hocr Islandora IIIF: Add missing null check. --- .../islandora_iiif/src/Plugin/views/style/IIIFManifest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php index ebf5a9225..f0aca47da 100644 --- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php +++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php @@ -423,7 +423,9 @@ protected function getOcrUrl(EntityInterface $entity) { if (!is_null($ocr_field_name)) { $ocrs = $ocr_entity->{$ocr_field_name}; $ocr = $ocrs[0] ?? FALSE; - $ocr_url = $ocr->entity->createFileUrl(FALSE); + if ($ocr) { + $ocr_url = $ocr->entity->createFileUrl(FALSE); + } } } elseif ($structured_text_term = $this->getStructuredTextTerm()) {