From 60e730afa61d87ee9969402c1c8f00b0464e71aa Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Fri, 30 Apr 2021 17:55:10 +0000 Subject: [PATCH 1/4] added tokens to handle islandora > media operation --- islandora.tokens.inc | 124 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 islandora.tokens.inc diff --git a/islandora.tokens.inc b/islandora.tokens.inc new file mode 100644 index 000000000..e5561d632 --- /dev/null +++ b/islandora.tokens.inc @@ -0,0 +1,124 @@ + t('Islandora Tokens'), + 'description' => t('Tokens for Islandora objects.'), + ]; + $node['media-thumbnail-image:url'] = [ + 'name' => t('Media: Thumbnail Image URL.'), + 'description' => t('URL of Thumbnail Image associated with Islandora Object via Media.'), + ]; + + $node['media-thumbnail-image:alt'] = [ + 'name' => t('Alternative text for Media: Thumbnail Image.'), + 'description' => t('Alternative text for Thumbnail Image associated with Islandora Object via Media.'), + ]; + + // Deprecated in favour if hyphenated version. + $node['media_thumbnail_image:url'] = [ + 'name' => t('Media: Thumbnail Image URL.'), + 'description' => t('Deprecated: URL of Thumbnail Image associated with Islandora Object via Media.'), + ]; + + // Deprecated in favour if hyphenated version. + $node['media_thumbnail_image:alt'] = [ + 'name' => t('Alternative text for Media: Thumbnail Image.'), + 'description' => t('Deprecated: Alternative text for Thumbnail Image associated with Islandora Object via Media.'), + ]; + + + return [ + 'types' => ['islandoratokens' => $type], + 'tokens' => ['islandoratokens' => $node], + ]; +} + +/** + * Implements hook_tokens(). + */ +function islandora_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) { + $replacements = []; + if ($type == 'islandoratokens' && !empty($data['node'])) { + if (!is_array($tokens) || empty($tokens)) { + \Drupal::logger('controlled_access_terms') + ->alert('Tokens not correct format: @tokens', [ + '@tokens' => print_r($tokens, 1), + ]); + return; + } + foreach ($tokens as $name => $original) { + switch ($name) { + case 'media-thumbnail-image:url': + case 'media_thumbnail_image:url': + $term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage'); + $media = $islandoraUtils->getMediaWithTerm($data['node'], $term); + // Is there media? + // @todo: is this single or multiple? + if ($media) { + $file = controlled_access_terms_image_from_media($media); + if (!empty($file)) { + $url = $file->url(); + $replacements[$original] = $url; + } + } + break; + + case 'media-thumbnail-image:alt': + case 'media_thumbnail_image:alt': + $alt = ''; + $islandoraUtils = \Drupal::service('islandora.utils'); + $term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage'); + $media = $islandoraUtils->getMediaWithTerm($data['node'], $term); + // Is there media? + // @todo: is this single or multiple? + if ($media) { + // Is the media an image? + if (isset($media->field_media_image)) { + $alt = $media->field_media_image[0]->alt; + } + } + // @todo: get alt from original or service file, if thumbnail alt is empty. + $replacements[$original] = $alt; + break; + } + } + } + return $replacements; +} + +/** + * Get File referenced by given Media. + * + * @todo: Add to IslandoraUtils? + * @todo: Had to remove typehint MediaInterface from function. + * + * @param \Drupal\media\MediaInterface $media + * The Media to operate on. + * + * @return mixed + * \Drupal\file\FileInterface on success, NULL otherwise. + */ +function controlled_access_terms_image_from_media($media) { + $files = []; + if ($media->hasField('field_media_image')) { + $files = $media->get('field_media_image')->referencedEntities(); + } + return !empty($files) ? reset($files) : NULL; +} + From 153d1d6d8c7f283a3281a2cd5015b6b3edf9b208 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Mon, 3 May 2021 18:27:49 +0000 Subject: [PATCH 2/4] used a more specific parameter class, also fixed missing variable --- islandora.tokens.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/islandora.tokens.inc b/islandora.tokens.inc index e5561d632..6152d3f8e 100644 --- a/islandora.tokens.inc +++ b/islandora.tokens.inc @@ -62,6 +62,7 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl ]); return; } + $islandoraUtils = \Drupal::service('islandora.utils'); foreach ($tokens as $name => $original) { switch ($name) { case 'media-thumbnail-image:url': @@ -82,7 +83,6 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl case 'media-thumbnail-image:alt': case 'media_thumbnail_image:alt': $alt = ''; - $islandoraUtils = \Drupal::service('islandora.utils'); $term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage'); $media = $islandoraUtils->getMediaWithTerm($data['node'], $term); // Is there media? @@ -108,7 +108,7 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl * @todo: Add to IslandoraUtils? * @todo: Had to remove typehint MediaInterface from function. * - * @param \Drupal\media\MediaInterface $media + * @param \Drupal\islandora\MediaSource\MediaSourceService $media * The Media to operate on. * * @return mixed From 8d08f24b32843ac0d87b6f1462626452ad1bc2e2 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Thu, 6 May 2021 18:24:31 +0000 Subject: [PATCH 3/4] changed the logger type from controlled_access_terms to islandora and removed the controlled_access_terms_image_from_media method --- islandora.tokens.inc | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/islandora.tokens.inc b/islandora.tokens.inc index 6152d3f8e..a75207aed 100644 --- a/islandora.tokens.inc +++ b/islandora.tokens.inc @@ -56,7 +56,7 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl $replacements = []; if ($type == 'islandoratokens' && !empty($data['node'])) { if (!is_array($tokens) || empty($tokens)) { - \Drupal::logger('controlled_access_terms') + \Drupal::logger('islandora') ->alert('Tokens not correct format: @tokens', [ '@tokens' => print_r($tokens, 1), ]); @@ -72,7 +72,7 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl // Is there media? // @todo: is this single or multiple? if ($media) { - $file = controlled_access_terms_image_from_media($media); + $file = \Drupal::service('islandora.media_source_service')->getSourceFile($media); if (!empty($file)) { $url = $file->url(); $replacements[$original] = $url; @@ -101,24 +101,3 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl } return $replacements; } - -/** - * Get File referenced by given Media. - * - * @todo: Add to IslandoraUtils? - * @todo: Had to remove typehint MediaInterface from function. - * - * @param \Drupal\islandora\MediaSource\MediaSourceService $media - * The Media to operate on. - * - * @return mixed - * \Drupal\file\FileInterface on success, NULL otherwise. - */ -function controlled_access_terms_image_from_media($media) { - $files = []; - if ($media->hasField('field_media_image')) { - $files = $media->get('field_media_image')->referencedEntities(); - } - return !empty($files) ? reset($files) : NULL; -} - From c975137614cbc7ee4cedfcff886229b30e41b698 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Tue, 1 Jun 2021 16:31:12 +0000 Subject: [PATCH 4/4] phpcs fixes for #1171 --- islandora.tokens.inc | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/islandora.tokens.inc b/islandora.tokens.inc index a75207aed..78f75aa67 100644 --- a/islandora.tokens.inc +++ b/islandora.tokens.inc @@ -7,10 +7,7 @@ * This file provides islandora tokens. */ -use Drupal\media\Entity\Media; -use Drupal\file\Entity\File; use Drupal\Core\Render\BubbleableMetadata; -use Drupal\islandora\IslandoraUtils; /** * Implements hook_token_info(). @@ -42,7 +39,6 @@ function islandora_token_info() { 'description' => t('Deprecated: Alternative text for Thumbnail Image associated with Islandora Object via Media.'), ]; - return [ 'types' => ['islandoratokens' => $type], 'tokens' => ['islandoratokens' => $node], @@ -57,9 +53,11 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl if ($type == 'islandoratokens' && !empty($data['node'])) { if (!is_array($tokens) || empty($tokens)) { \Drupal::logger('islandora') - ->alert('Tokens not correct format: @tokens', [ - '@tokens' => print_r($tokens, 1), - ]); + ->alert( + 'Tokens not correct format: @tokens', [ + '@tokens' => print_r($tokens, 1), + ] + ); return; } $islandoraUtils = \Drupal::service('islandora.utils'); @@ -70,7 +68,7 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl $term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage'); $media = $islandoraUtils->getMediaWithTerm($data['node'], $term); // Is there media? - // @todo: is this single or multiple? + // @todo is this single or multiple? if ($media) { $file = \Drupal::service('islandora.media_source_service')->getSourceFile($media); if (!empty($file)) { @@ -86,14 +84,15 @@ function islandora_tokens($type, $tokens, array $data, array $options, Bubbleabl $term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage'); $media = $islandoraUtils->getMediaWithTerm($data['node'], $term); // Is there media? - // @todo: is this single or multiple? + // @todo is this single or multiple? if ($media) { // Is the media an image? if (isset($media->field_media_image)) { $alt = $media->field_media_image[0]->alt; } } - // @todo: get alt from original or service file, if thumbnail alt is empty. + // @todo get alt from original or service file, if thumbnail + // alt is empty. $replacements[$original] = $alt; break; }