From f69aae099db4db0b394dda948c4a811e99707efa Mon Sep 17 00:00:00 2001
From: Alexander O'Neill <aloneill@gmail.com>
Date: Fri, 26 Aug 2022 17:52:20 +0000
Subject: [PATCH] WIP: Add hOCR file stream to IIIF Manifest.

---
 .../src/Plugin/views/style/IIIFManifest.php   | 23 +++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

diff --git a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php
index b06b0f5d..ad5ea178 100644
--- a/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php
+++ b/modules/islandora_iiif/src/Plugin/views/style/IIIFManifest.php
@@ -191,20 +191,25 @@ class IIIFManifest extends StylePluginBase {
     $canvases = [];
     foreach (array_filter(array_values($this->options['iiif_tile_field'])) as $iiif_tile_field) {
       $viewsField = $this->view->field[$iiif_tile_field];
-      $ocrField = array_pop(array_filter(array_values($this->options['iiif_ocr_file_field'])));
+      $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;
       $entity = $viewsField->getEntity($row);
 
       if (isset($entity->{$viewsField->definition['field_name']})) {
 
         /** @var \Drupal\Core\Field\FieldItemListInterface $images */
-        $images = $entity->{$viewsField->definition['field_name']};
+        $images = $entity->{$viewsField->definition['field_name']}; 
         foreach ($images as $image) {
           if (!$image->entity->access('view')) {
             // If the user does not have permission to view the file, skip it.
             continue;
           }
+
+        $ocrs = $entity->{$ocrField->definition['field_name']}; 
+
           // Create the IIIF URL for this file
           // Visiting $iiif_url will resolve to the info.json for the image.
+          $ocr = isset($ocrs[$i]) ? $ocrs[$i] : NULL;
           $file_url = $image->entity->createFileUrl(FALSE);
           $mime_type = $image->entity->getMimeType();
           $iiif_url = rtrim($iiif_address, '/') . '/' . urlencode($file_url);
@@ -242,8 +247,7 @@ class IIIFManifest extends StylePluginBase {
               }
             }
           }
-
-          $canvases[] = [
+          $tmp_canvas = [
             // @see https://iiif.io/api/presentation/2.1/#canvas
             '@id' => $canvas_id,
             '@type' => 'sc:Canvas',
@@ -272,6 +276,17 @@ class IIIFManifest extends StylePluginBase {
               ],
             ],
           ];
+
+          if (isset($ocr)) {
+            $tmp_canvas['seeAlso'] = [
+              '@id' => $ocr->entity->createFileUrl(FALSE),
+              'format' => 'text/vnd.hocr+html',
+              'profile' => 'http://kba.cloud/hocr-spec',
+              'label' => 'hOCR embedded text',
+            ];
+          }
+
+          $canvases[] = $tmp_canvas;
         }
       }
     }