-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added tokens to handle islandora > media operation #836
Changes from 1 commit
60e730a
153d1d6
8d08f24
c975137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains islandora.tokens.inc. | ||
* | ||
* 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(). | ||
*/ | ||
function islandora_token_info() { | ||
$type = [ | ||
'name' => 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'); | ||
dannylamb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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); | ||
dannylamb marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can replace this with
and delete the |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably can ditch this for the MediaSourceService, but if not, you'd wanna rename it :) |
||
$files = []; | ||
if ($media->hasField('field_media_image')) { | ||
$files = $media->get('field_media_image')->referencedEntities(); | ||
} | ||
return !empty($files) ? reset($files) : NULL; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
controlled_access_terms
should beislandora
here.