-
Notifications
You must be signed in to change notification settings - Fork 31
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
Islandora tokens 1171 #66
Changes from 2 commits
defa6c9
890a16a
8af389e
3fab7ad
f08d153
f8e3452
4d59f90
acc7f39
2314594
087fffc
87be45e
5ac469a
d7c4b36
5aa8c34
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,374 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains controlled_access_terms.tokens.inc. | ||
* | ||
* This file provides islandora tokens. | ||
*/ | ||
|
||
use Drupal\taxonomy\Entity\Term; | ||
use Drupal\media\Entity\Media; | ||
use Drupal\file\Entity\File; | ||
use Drupal\controlled_access_terms\EDTFUtils; | ||
use Drupal\Core\Render\BubbleableMetadata; | ||
use Drupal\islandora\IslandoraUtils; | ||
|
||
/** | ||
* Implements hook_token_info(). | ||
*/ | ||
function controlled_access_terms_token_info() { | ||
$type = [ | ||
'name' => t('Islandora Tokens'), | ||
'description' => t('Tokens for Islandora objects.'), | ||
]; | ||
$node['title'] = [ | ||
'name' => t("Node Title"), | ||
'description' => t("The node's title"), | ||
]; | ||
$node['agent_author'] = [ | ||
'name' => t("Author"), | ||
'description' => t("The node's Author linked agents"), | ||
]; | ||
$node['agent_contributor'] = [ | ||
'name' => t("Contributors"), | ||
'description' => t("The node's Contributor linked agents"), | ||
]; | ||
$node['agent_publisher'] = [ | ||
'name' => t("Publishers"), | ||
'description' => t("The node's Publishers linked agents"), | ||
]; | ||
$node['agent_creators'] = [ | ||
'name' => t("Creators"), | ||
'description' => t("The node's Creators linked agents"), | ||
]; | ||
$node['publication_date'] = [ | ||
'name' => t("Publication date"), | ||
'description' => t('Show the "Date Created" into YYYY/MM/DD format (handles EDTF format)'), | ||
]; | ||
// Move this field to our own baby asu_islandora_tokens module. | ||
// $node['copyright_date'] = [ | ||
// 'name' => t("Copyright date"), | ||
// 'description' => t('Show the "Copyright Date" into YYYY/MM/DD format (handles EDTF format)'), | ||
// ]; | ||
$node['pdf_url'] = [ | ||
'name' => t("PDF Url"), | ||
'description' => t('URL to related media file if "Original file" is a PDF file'), | ||
]; | ||
$node['media-thumbnail-image:url'] = [ | ||
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.
|
||
'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 controlled_access_terms_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 'title': | ||
$replacements[$original] = $data['node']->getTitle(); | ||
break; | ||
|
||
case 'agent_author': | ||
// For the Linked agent field, the Author/s are specified | ||
// by the relationship "aut" for Author. | ||
$replacements[$original] = _get_term_with_rel_type($data['node'], 'field_linked_agent', 'relators:aut', TRUE); | ||
break; | ||
|
||
case 'agent_contributor': | ||
// For the Linked agent field, the Contributor/s are specified | ||
// by the relationship "ctb". | ||
$replacements[$original] = _get_term_with_rel_type($data['node'], 'field_linked_agent', 'relators:ctb', TRUE); | ||
break; | ||
|
||
case 'agent_creators': | ||
// For the Linked agent field, Creators are any relationship value. | ||
$replacements[$original] = _get_term_with_rel_type($data['node'], 'field_linked_agent', ''); | ||
break; | ||
|
||
case 'agent_publisher': | ||
// For the Linked agent field, Publishers are any relationship value. | ||
$replacements[$original] = _get_term_with_rel_type($data['node'], 'field_linked_agent', 'relators:pbl'); | ||
break; | ||
|
||
case 'publication_date': | ||
$replacements[$original] = _normalize_date_format($data['node'], 'field_edtf_date_created', $original); | ||
break; | ||
|
||
//// Move this one to our own baby islandora_tokens module. | ||
// case 'copyright_date': | ||
// $replacements[$original] = _normalize_date_format($data['node'], 'field_edtf_copyright_date', $original); | ||
// break; | ||
|
||
case 'pdf_url': | ||
$replacements[$original] = ' ' . _url_to_service_file_media_by_mimetype($data['node'], 'application/pdf'); | ||
break; | ||
|
||
case 'media-thumbnail-image:url': | ||
case 'media_thumbnail_image:url': | ||
$term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage'); | ||
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. $islandoraUtils doesn't exist here. Needs $islandoraUtils = \Drupal::service('islandora.utils'); |
||
$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; | ||
} | ||
|
||
/** | ||
* Helper function to load values for a taxonomy term with a relationship type. | ||
* | ||
* @param object $node | ||
* A core drupal node object. | ||
* @param string $field_name | ||
* The name of the node's field to check for the specific relationship. | ||
* @param string $relation_type | ||
* Optional value to check the rel_type of the taxonomy term against. When | ||
* not provided, any terms returned for the field will match. | ||
* @param bool $remove_comma | ||
* Will flip the string parts of a CSV. | ||
* | ||
* @return string | ||
* The tokenized value for the given data. | ||
*/ | ||
function _get_term_with_rel_type($node, $field_name, $relation_type = '', $remove_comma = FALSE) { | ||
$matches = []; | ||
$field = ($node->hasField($field_name) ? $node->get($field_name) : NULL); | ||
if (is_object($field)) { | ||
$tids = $field->getValue(); | ||
foreach ($tids as $tid) { | ||
// Inspect the taxonomy term. | ||
if ( | ||
is_array($tid) && | ||
array_key_exists('target_id', $tid) | ||
) { | ||
if ($relation_type && array_key_exists('rel_type', $tid)) { | ||
if ($tid['rel_type'] == $relation_type) { | ||
$term = Term::load($tid['target_id']); | ||
if ($term) { | ||
$matches[] = controlled_access_terms_remove_comma($term->getName(), $remove_comma); | ||
} | ||
} | ||
} | ||
else { | ||
$term = Term::load($tid['target_id']); | ||
if ($term) { | ||
$matches[] = controlled_access_terms_remove_comma($term->getName(), $remove_comma); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return implode(", ", $matches); | ||
} | ||
|
||
/** | ||
* Helper function to normalize a date value that could be an EDTF date value. | ||
* | ||
* @param object $node | ||
* A core drupal node object. | ||
* @param string $field_name | ||
* The name of the node's field to check for the specific relationship. | ||
* @param string $original | ||
* This is the token value that is being processed - for logging purposes. | ||
* | ||
* @return string | ||
* The tokenized value for the given data. | ||
* | ||
* @throws Exception | ||
*/ | ||
function _normalize_date_format($node, $field_name, $original) { | ||
$retval = ''; | ||
try { | ||
$created_date = ($node->hasField($field_name) ? $node->get($field_name) : NULL); | ||
if (!is_null($created_date)) { | ||
if ( | ||
is_array($created_date->getValue($field_name)) && | ||
array_key_exists(0, $created_date->getValue($field_name)) && | ||
is_array($created_date->getValue($field_name)[0]) && | ||
array_key_exists('value', $created_date->getValue($field_name)[0]) | ||
) { | ||
$v = $created_date->getValue($field_name)[0]['value']; | ||
if (strlen($v) < 6) { | ||
throw new Exception('Date does not have enough digits.'); | ||
} | ||
$date_val = new DateTime($v); | ||
$retval = $date_val->format('Y/m/d'); | ||
} | ||
} | ||
} | ||
catch (Exception $e) { | ||
if (strpos($field_name, 'edtf') != FALSE) { | ||
$date = $node->$field_name->value; | ||
$iso = EDTFUtils::iso8601Value($date); | ||
$components = explode('-', $iso); | ||
$year = array_shift($components); | ||
if (strpos($year, 'T') != FALSE) { | ||
$year_parts = explode('T', $year); | ||
$year = $year_parts[0]; | ||
} | ||
if (is_numeric($year)) { | ||
return $year; | ||
} | ||
} | ||
// Date value could not be converted to a string. | ||
\Drupal::logger('controlled_access_terms')->notice( | ||
'During token generation, a date ' . | ||
'value: @date could not be converted to a DateTime object, used token "@token_value".', | ||
[ | ||
'@date' => @$created_date->getValue($field_name)[0]['value'], | ||
'@token_value' => $original, | ||
] | ||
); | ||
} | ||
return $retval; | ||
} | ||
|
||
/** | ||
* Gets Original File PDF file URL. | ||
* | ||
* @param object $node | ||
* A core drupal node object. | ||
* @param string $mime_type | ||
* The name of the node's field to check for the specific relationship. | ||
* | ||
* @return string | ||
* The tokenized value for the given data. | ||
*/ | ||
function _url_to_service_file_media_by_mimetype($node, $mime_type) { | ||
$islandora_utils = \Drupal::service('islandora.utils'); | ||
$origfile_term = $islandora_utils->getTermForUri('http://pcdm.org/use#OriginalFile'); | ||
$origfile_media = $islandora_utils->getMediaWithTerm($node, $origfile_term); | ||
// Get the media file's mime_type value. | ||
if (is_object($origfile_media)) { | ||
$origfile_mime_type = ($origfile_media->hasField('field_mime_type')) ? | ||
$origfile_media->get('field_mime_type')->getValue() : NULL; | ||
$origfile_mime_type = (is_array($origfile_mime_type) && | ||
array_key_exists(0, $origfile_mime_type) && | ||
is_array($origfile_mime_type[0]) && | ||
array_key_exists('value', $origfile_mime_type[0])) ? | ||
$origfile_mime_type[0]['value'] : ''; | ||
// Compare the media file's mime_type to the given value. | ||
if ($origfile_mime_type == $mime_type) { | ||
$vid = $origfile_media->id(); | ||
if (!is_null($vid)) { | ||
$media = Media::load($vid); | ||
$bundle = $media->bundle(); | ||
// Since this is Islandora and we assume the Original File is a | ||
// Document type... but doing it dynamically. | ||
$fid = $media->get('field_media_' . $bundle)->getValue(); | ||
$fid_value = (is_array($fid) && array_key_exists(0, $fid) && | ||
array_key_exists('target_id', $fid[0])) ? | ||
$fid[0]['target_id'] : NULL; | ||
if (!is_null($fid_value)) { | ||
$file = File::load($fid_value); | ||
$url = $file->getFileUri(); | ||
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. $url returned here is fedora://filepath. Needs to include overall site domain. |
||
return $url; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Conditionally removes any commas and reverse the order of the string values. | ||
* | ||
* @param string $input | ||
* The string value to potentially remove and flip parts. | ||
* @param bool $remove_comma | ||
* Whether or not to remove the comma and flip the parts. | ||
* | ||
* @return string | ||
* The value of the label. | ||
*/ | ||
function controlled_access_terms_remove_comma($input, $remove_comma = FALSE) { | ||
if ($remove_comma) { | ||
$parts = explode(",", $input); | ||
$parts = array_reverse($parts); | ||
return implode(" ", $parts); | ||
} | ||
else { | ||
return $input; | ||
} | ||
} | ||
|
||
/** | ||
* 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. We can't rely on Islandora services because this module may be used independently of it. I would crib from Islandora's Media Source Service for a Drupal solution that supports multiple Media Types (See both https://github.com/Islandora/islandora/blob/8.x-1.x/src/MediaSource/MediaSourceService.php#L121-L133 and https://github.com/Islandora/islandora/blob/8.x-1.x/src/MediaSource/MediaSourceService.php#L97-L107). 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. I didn't write this part, but I think I can swap out that fixed field-name with the bundle config media type fieldname without breaking it. fingers-crossed. 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. Or maybe move the chunks for getting files from Media to 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. ok - this makes sense... I can make those changes... so, I infer that there would need to be synchronized pull requests after this so that Should I just make note of this in both pull requests' descriptions (that there is a related pull request on the other module)? |
||
$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.
This fella ought to be moved into the islandora module since it does the whole 'media lookup by media_use term' dealio.
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.
This guy still needs to get moved over into the islandora module, or you put a conditional around it to check for the islandora module. At least then it'll be a soft dependency instead of an undeclared hard dependency.
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.
Ok -- this token has now been moved over to the
islandora_tokens_1171
branch of the islandora module https://github.com/asulibraries/islandora/tree/islandora_tokens_1171 (as well as renaming the helper function from "controlled_access_terms_*" to lookup the media by mime type).