-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIIIFManifest.php
More file actions
729 lines (655 loc) · 24.1 KB
/
Copy pathIIIFManifest.php
File metadata and controls
729 lines (655 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
<?php
namespace Drupal\islandora_iiif\Plugin\views\style;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\taxonomy\TermInterface;
use Drupal\views\Plugin\views\style\StylePluginBase;
use Drupal\views\ResultRow;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\termwithuri_condition\Utils;
/**
* Provide serializer format for IIIF Manifest.
*
* @ingroup views_style_plugins
*
* @ViewsStyle(
* id = "iiif_manifest",
* title = @Translation("IIIF Manifest"),
* help = @Translation("Display images as an IIIF Manifest."),
* display_types = {"data"}
* )
*/
class IIIFManifest extends StylePluginBase {
/**
* {@inheritdoc}
*/
protected $usesRowPlugin = TRUE;
/**
* {@inheritdoc}
*/
protected $usesGrouping = FALSE;
/**
* The allowed formats for this serializer. Default to only JSON.
*
* @var array
*/
protected $formats = ['json'];
/**
* The serializer which serializes the views result.
*
* @var \Symfony\Component\Serializer\Serializer
*/
protected $serializer;
/**
* The request service.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* This module's config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $iiifConfig;
/**
* The Drupal Entity Type Manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The Drupal Filesystem.
*
* @var \Drupal\Core\File\FileSystem
*/
protected $fileSystem;
/**
* The Guzzle HTTP Client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Module Handler for running hooks.
*
* @var \Drupal\Core\Extention\ModuleHandlerInterface
*/
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}
*/
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) {
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;
$this->moduleHandler = $moduleHandler;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$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'),
$container->get('module_handler')
);
}
/**
* Return the request property.
*
* @return \Symfony\Component\HttpFoundation\Request
* The Symfony request object
*/
public function getRequest() {
return $this->request;
}
/**
* {@inheritdoc}
*/
public function render() {
$json = [];
$iiif_address = $this->iiifConfig->get('iiif_server');
if (!is_null($iiif_address) && !empty($iiif_address)) {
// Get the current URL being requested.
$request_host = $this->request->getSchemeAndHttpHost();
$request_host = str_replace('http://', 'https://', $request_host);
$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('/', trim($request_url, '/'));
array_pop($url_components);
$content_path = '/' . implode('/', $url_components);
$iiif_base_id = "{$request_host}{$content_path}";
$display = $this->iiifConfig->get('show_title');
switch ($display) {
case 'none':
$label = '';
break;
case 'view':
$label = $this->view->getTitle();
break;
case 'node':
$label = $this->getEntityTitle($content_path);
break;
default:
$label = $this->t("IIIF Manifest");
}
$url_parts = explode('/', trim($request_url, '/'));
$node_id = end($url_parts);
if (!is_numeric($node_id)) {
$node_id = end($url_components);
}
$metadata = [];
if ($node_id && is_numeric($node_id)) {
$node = $this->entityTypeManager->getStorage('node')->load($node_id);
if ($node) {
$metadata[] = [
'label' => 'Title',
'value' => $node->getTitle(),
];
if ($node->hasField('field_rights') && !$node->get('field_rights')->isEmpty()) {
$rights_value = $node->get('field_rights')->entity ? $node->get('field_rights')->entity->label() : $node->get('field_rights')->getString();
$metadata[] = [
'label' => 'Rights Statement',
'value' => $rights_value,
];
}
}
}
// @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' => $label,
'metadata' => $metadata,
'@context' => 'http://iiif.io/api/presentation/2/context.json',
// @see https://iiif.io/api/presentation/2.1/#sequence
'sequences' => [
[
'@context' => 'http://iiif.io/api/presentation/2/context.json',
'@id' => $iiif_base_id . '/sequence/normal',
'@type' => 'sc:Sequence',
],
],
];
// 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);
foreach ($canvases as $tile_source) {
$json['sequences'][0]['canvases'][] = $tile_source;
}
}
}
unset($this->view->row_index);
$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);
return $this->serializer->serialize($json, $content_type, ['views_style_plugin' => $this]);
}
/**
* Render array from views result row.
*
* @param \Drupal\views\ResultRow $row
* Result row.
* @param string $iiif_address
* The URL to the IIIF server endpoint.
* @param string $iiif_base_id
* The URL for the request, minus the last part of the URL,
* which is likely "manifest".
*
* @return array
* List of IIIF URLs to display in the Openseadragon viewer.
*/
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];
$entity = $viewsField->getEntity($row);
if (isset($entity->{$viewsField->definition['field_name']})) {
/** @var \Drupal\Core\Field\FieldItemListInterface $images */
$images = $entity->{$viewsField->definition['field_name']};
// phpcs:ignore -- Unused variable $i.
foreach ($images as $i => $image) {
if (!$image->entity->access('view')) {
// If the user does not have permission to view the file, skip it.
continue;
}
// Create the IIIF URL for this file
// Visiting $iiif_url will resolve to the info.json for the image.
if ($this->iiifConfig->get('use_relative_paths')) {
$file_url = ltrim($image->entity->createFileUrl(TRUE), '/');
}
else {
$file_url = $image->entity->createFileUrl(FALSE);
}
$mime_type = $image->entity->getMimeType();
$iiif_url = rtrim($iiif_address, '/') . '/' . urlencode($file_url);
// Create the necessary ID's for the canvas and annotation.
$canvas_id = $iiif_base_id . '/canvas/' . $entity->id();
$annotation_id = $iiif_base_id . '/annotation/' . $entity->id();
[$width, $height] = $this->getCanvasDimensions($iiif_url, $image, $mime_type);
$tmp_canvas = [
// @see https://iiif.io/api/presentation/2.1/#canvas
'@id' => $canvas_id,
'@type' => 'sc:Canvas',
'label' => $image->entity->label(),
'height' => $height,
'width' => $width,
// @see https://iiif.io/api/presentation/2.1/#image-resources
'images' => [
[
'@id' => $annotation_id,
"@type" => "oa:Annotation",
'motivation' => 'sc:painting',
'resource' => [
'@id' => $iiif_url . '/full/full/0/default.jpg',
"@type" => "dctypes:Image",
'format' => $mime_type,
'height' => $height,
'width' => $width,
'service' => [
'@id' => $iiif_url,
'@context' => 'http://iiif.io/api/image/2/context.json',
'profile' => 'http://iiif.io/api/image/2/profiles/level2.json',
],
],
'on' => $canvas_id,
],
],
];
$node_id = NULL;
if (preg_match('/\/node\/(\d+)/', $iiif_base_id, $matches)) {
$node_id = $matches[1];
}
if ($ocr_url = $this->getOcrUrl($entity, $node_id)) {
$ocr_url = str_replace('http://', 'https://', $ocr_url);
$tmp_canvas['seeAlso'] = [
'@id' => $ocr_url,
'format' => 'text/vnd.hocr+html',
'profile' => 'http://kba.cloud/hocr-spec',
'label' => 'hOCR embedded text',
];
}
// Give other modules a chance to alter the canvas.
$alter_options = [
'options' => $this->options,
'views_plugin' => $this,
];
$this->moduleHandler->alter('islandora_iiif_manifest_canvas', $tmp_canvas, $row, $alter_options);
$canvases[] = $tmp_canvas;
}
}
}
return $canvases;
}
/**
* Try to fetch the IIIF metadata for the image.
*
* @param string $iiif_url
* Base URL of the canvas.
* @param \Drupal\Core\Field\FieldItemInterface $image
* The image field.
* @param string $mime_type
* The mime type of the image.
*
* @return [string]
* The width and height of the image.
*/
protected function getCanvasDimensions(string $iiif_url, FieldItemInterface $image, string $mime_type) {
if (isset($image->width) && is_numeric($image->width)
&& isset($image->height) && is_numeric($image->height)) {
return [intval($image->width), intval($image->height)];
}
try {
// For dsu-utsc.
if (!empty(\Drupal::hasService('jwt.authentication.jwt'))) {
$jwtService = \Drupal::service('jwt.authentication.jwt');
$token = $jwtService->generateToken();
$info_json = $this->httpClient->get($iiif_url, [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
])->getBody();
}
else {
$info_json = $this->httpClient->get($iiif_url)->getBody();
}
$resource = json_decode($info_json, TRUE);
$width = $resource['width'];
$height = $resource['height'];
}
catch (ClientException | ServerException | ConnectException $e) {
// If we couldn't get the info.json from IIIF
// try seeing if we can get it from Drupal.
if (empty($width) || empty($height)) {
// Get the image properties so we know the image width/height.
$properties = $image->getProperties();
$width = $properties['width'] ?? 0;
$height = $properties['height'] ?? 0;
// If this is a TIFF AND we don't know the width/height
// see if we can get the image size via PHP's core function.
if ($mime_type === 'image/tiff' && (!$width || !$height)) {
$uri = $image->entity->getFileUri();
$path = $this->fileSystem->realpath($uri);
$image_size = getimagesize($path);
if ($image_size) {
$width = $image_size[0];
$height = $image_size[1];
}
}
}
}
return [$width, $height];
}
/**
* Retrieves a URL text with positional data such as hOCR.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity at the current row.
* @param int $id
* The ID of the book node.
*
* @return string|false
* The absolute URL of the current row's structured text,
* or FALSE if none.
*/
protected function getOcrUrl(EntityInterface $entity, int $id) {
$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 = $entity;
$ocr_field_name = $ocrField->definition['field_name'];
if (!is_null($ocr_field_name)) {
$ocrs = $ocr_entity->{$ocr_field_name};
$ocr = $ocrs[0] ?? FALSE;
if ($ocr) {
$ocr_url = $ocr->entity->createFileUrl(FALSE);
}
}
}
elseif ($structured_text_term = $this->getStructuredTextTerm()) {
$parent_node = $this->getParentNode($entity, $id);
// For items without parent node, return their id as the parent node
// This is the case for images with ocr,
// Have not tested this case fully though!
if ($parent_node == NULL) {
return $parent_node;
}
$ocr_entity_array = 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);
}
}
return $ocr_url;
}
/**
* Gets nodes that a media belongs to.
*
* // phpcs:ignore -- Doc comment for parameter $media does not match actual variable name $id
* @param \Drupal\media\MediaInterface $media
* The Media whose node you are searching for.
* @param int $id
* The ID of the book node.
* // phpcs:ignore -- Doc comment for parameter $entity does not match actual variable name <undefined>
* @param EntityInterface $entity
* The entity.
*
* @return \Drupal\node\NodeInterface
* Parent node.
*
* @throws \Drupal\Core\TypedData\Exception\MissingDataException
* Method $field->first() throws if data structure is unset and no item can
* be created.
*/
protected function getParentNode(EntityInterface $entity, int $id) {
if (!$id) {
return NULL;
}
$query = $this->entityTypeManager->getStorage('node')->getQuery();
$nids = $query->accessCheck(TRUE)->condition('field_part_of', $id)->execute();
if (!empty($nids)) {
foreach ($nids as $nid) {
$node = $this->entityTypeManager->getStorage('node')->load($nid);
$media_ids = array_map(function ($item) {
return $item['target_id'];
}, $node->get('field_islandora_object_media')->getValue());
if (in_array($entity->id(), $media_ids)) {
return $node;
}
}
}
return NULL;
}
/**
* 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}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['iiif_tile_field'] = ['default' => ''];
$options['iiif_ocr_file_field'] = ['default' => ''];
return $options;
}
/**
* Add the configured search endpoint to the manifest.
*
* @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'] ?? NULL;
if ($hocr_search_path) {
$hocr_search_url = $url_base . '/' . ltrim($hocr_search_path, '/');
$hocr_search_url = str_replace('%node', $url_components[1], $hocr_search_url);
$hocr_search_url = str_replace('http://', 'https://', $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",
// phpcs:ignore -- t() calls should be avoided in classes, use \Drupal\Core\StringTranslation\StringTranslationTrait and $this->t() instead
"label" => t("Search inside this work"),
];
}
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$field_options = [];
$fields = $this->displayHandler->getHandlers('field');
$islandora_default_file_fields = [
'field_media_file',
'field_media_image',
];
$file_views_field_formatters = [
// Image formatters.
'image', 'image_url',
// File formatters.
'file_default', 'file_url_plain',
];
/** @var \Drupal\views\Plugin\views\field\FieldPluginBase[] $fields */
foreach ($fields as $field_name => $field) {
// If this is a known Islandora file/image field
// OR it is another/custom field add it as an available option.
// @todo find better way to identify file fields
// Currently $field->options['type'] is storing the "formatter" of the
// file/image so there are a lot of possibilities.
// The default formatters are 'image' and 'file_default'
// so this approach should catch most...
if (in_array($field_name, $islandora_default_file_fields) ||
(!empty($field->options['type']) && in_array($field->options['type'], $file_views_field_formatters))) {
$field_options[$field_name] = $field->adminLabel();
}
}
// If no fields to choose from, add an error message indicating such.
if (count($field_options) == 0) {
$this->messenger->addMessage($this->t('No image or file fields were found in the View.
You will need to add a field to this View'), 'error');
}
$form['iiif_tile_field'] = [
'#title' => $this->t('Tile source field(s)'),
'#type' => 'checkboxes',
'#default_value' => $this->options['iiif_tile_field'],
'#description' => $this->t("The source of image for each entity."),
'#options' => $field_options,
// Only make the form element required if
// we have more than one option to choose from
// otherwise could lock up the form when setting up a View.
'#required' => count($field_options) > 0,
];
$form['iiif_ocr_file_field'] = [
'#title' => $this->t('Structured OCR data file field'),
'#type' => 'checkboxes',
'#default_value' => $this->options['iiif_ocr_file_field'],
'#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,
];
$form['structured_text_term'] = [
'#type' => 'entity_autocomplete',
'#target_type' => 'taxonomy_term',
'#title' => $this->t('Structured OCR text term'),
'#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.'),
];
$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 %node substitution where needed.<br>E.g., paged-content-search/%node"),
'#default_value' => $this->options['search_endpoint'],
'#required' => FALSE,
];
}
/**
* Returns an array of format options.
*
* @return string[]
* An array of the allowed serializer formats. In this case just JSON.
*/
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.
*/
/**
* Submit handler for options form.
*
* @codingStandardsIgnoreStart
*/
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'] = Utils::getUriForTerm($term);
$form_state->setValue('style_options', $style_options);
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 = Utils::getTermForUri($this->options['structured_text_term_uri']);
}
return $this->structuredTextTerm;
}
}