From defa6c99fa53a72d0eb441a0fbedcdda91d6a184 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Wed, 28 Apr 2021 18:37:12 +0000 Subject: [PATCH 01/13] tokens info and token hook handler for the merged works of Jonathan Hunt and ASU distinct implements of islandora_tokens modules for #1171 --- controlled_access_terms.tokens.inc | 290 +++++++++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 controlled_access_terms.tokens.inc diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc new file mode 100644 index 0000000..3fde72a --- /dev/null +++ b/controlled_access_terms.tokens.inc @@ -0,0 +1,290 @@ + 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'), + ]; + 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'])) { + 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; + } + } + } + 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(); + 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; + } +} From 890a16a08bb1f1a5952f66d5f6e9239310733e85 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Wed, 28 Apr 2021 19:45:02 +0000 Subject: [PATCH 02/13] moved relevant islandora_tokens code here for #1171 --- controlled_access_terms.tokens.inc | 84 ++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index 3fde72a..a555a97 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -12,6 +12,7 @@ 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(). @@ -54,6 +55,29 @@ function controlled_access_terms_token_info() { 'name' => t("PDF Url"), 'description' => t('URL to related media file if "Original file" is a PDF file'), ]; + $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], @@ -66,6 +90,13 @@ function controlled_access_terms_token_info() { 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': @@ -106,6 +137,39 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti 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'); + $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; } } } @@ -288,3 +352,23 @@ function controlled_access_terms_remove_comma($input, $remove_comma = FALSE) { 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) { + $files = []; + if ($media->hasField('field_media_image')) { + $files = $media->get('field_media_image')->referencedEntities(); + } + return !empty($files) ? reset($files) : NULL; +} From 8af389e01ff89c81d2177edeb39cc4989e9cff35 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Fri, 30 Apr 2021 18:01:56 +0000 Subject: [PATCH 03/13] moved tokens here from defunct islandora_tokens module --- controlled_access_terms.tokens.inc | 295 +++++++++++++++++++++++++++++ 1 file changed, 295 insertions(+) create mode 100644 controlled_access_terms.tokens.inc diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc new file mode 100644 index 0000000..c1e7781 --- /dev/null +++ b/controlled_access_terms.tokens.inc @@ -0,0 +1,295 @@ + 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)'), + ]; + $node['pdf_url'] = [ + 'name' => t("PDF Url"), + 'description' => t('URL to related media file if "Original file" is a PDF file'), + ]; + + 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; + + } + } + } + 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(); + 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; + } +} From 3fab7ad867fa8c14a9cb0071dcbf6a51a833a5ce Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Mon, 3 May 2021 15:22:57 +0000 Subject: [PATCH 04/13] added a comment --- controlled_access_terms.tokens.inc | 1 + 1 file changed, 1 insertion(+) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index c1e7781..14eba0b 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -239,6 +239,7 @@ function _normalize_date_format($node, $field_name, $original) { */ function _url_to_service_file_media_by_mimetype($node, $mime_type) { $islandora_utils = \Drupal::service('islandora.utils'); + // This term value could potentially be a config value. $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. From f08d153437bb833a394fe364afc6e0f2bdd5b4d5 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Thu, 13 May 2021 21:51:17 +0000 Subject: [PATCH 05/13] added file_create_url() to the media-thumbnail-image:url token, bugfix to islandoraUtils object variable for #1171 --- controlled_access_terms.tokens.inc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index a555a97..8c76713 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -140,6 +140,7 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti case 'media-thumbnail-image:url': case 'media_thumbnail_image:url': + $islandoraUtils = \Drupal::service('islandora.utils'); $term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage'); $media = $islandoraUtils->getMediaWithTerm($data['node'], $term); // Is there media? @@ -147,7 +148,7 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti if ($media) { $file = controlled_access_terms_image_from_media($media); if (!empty($file)) { - $url = $file->url(); + $url = file_create_url($file->url()); $replacements[$original] = $url; } } From f8e345294125b6a6177186eb3f5b3911f2d7aaf5 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Thu, 13 May 2021 22:38:37 +0000 Subject: [PATCH 06/13] bugfix to use D9 friendly file object methods for #1171 --- controlled_access_terms.tokens.inc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index 8c76713..d0e3481 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -147,8 +147,8 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti // @todo: is this single or multiple? if ($media) { $file = controlled_access_terms_image_from_media($media); - if (!empty($file)) { - $url = file_create_url($file->url()); + if (!is_null($file) && !empty($file)) { + $url = file_create_url($file->getFileUri()); // url()); $replacements[$original] = $url; } } From 4d59f909940660f0d4649f26b5890fcc0cc05d79 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Tue, 6 Jul 2021 22:45:20 +0000 Subject: [PATCH 07/13] phpcs - fixed all warnings and errors for #1171 --- controlled_access_terms.tokens.inc | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index d0e3481..a29631c 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -12,7 +12,6 @@ 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(). @@ -46,11 +45,6 @@ function controlled_access_terms_token_info() { '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'), @@ -77,7 +71,6 @@ function controlled_access_terms_token_info() { 'description' => t('Deprecated: Alternative text for Thumbnail Image associated with Islandora Object via Media.'), ]; - return [ 'types' => ['islandoratokens' => $type], 'tokens' => ['islandoratokens' => $node], @@ -129,11 +122,6 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti $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; @@ -144,11 +132,12 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti $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 = controlled_access_terms_image_from_media($media); if (!is_null($file) && !empty($file)) { - $url = file_create_url($file->getFileUri()); // url()); + // url()); + $url = file_create_url($file->getFileUri()); $replacements[$original] = $url; } } @@ -161,14 +150,14 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti $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; } @@ -357,8 +346,8 @@ function controlled_access_terms_remove_comma($input, $remove_comma = FALSE) { /** * Get File referenced by given Media. * - * @todo: Add to IslandoraUtils? - * @todo: Had to remove typehint MediaInterface from function. + * @todo Add to IslandoraUtils? + * @todo Had to remove typehint MediaInterface from function. * * @param \Drupal\media\MediaInterface $media * The Media to operate on. @@ -366,7 +355,7 @@ function controlled_access_terms_remove_comma($input, $remove_comma = FALSE) { * @return mixed * \Drupal\file\FileInterface on success, NULL otherwise. */ -function controlled_access_terms_image_from_media($media) { +function controlled_access_terms_image_from_media(MediaInterface $media) { $files = []; if ($media->hasField('field_media_image')) { $files = $media->get('field_media_image')->referencedEntities(); From acc7f3913b3621ba9dd603246f2656aea5a5d2f5 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Fri, 16 Jul 2021 00:28:26 +0000 Subject: [PATCH 08/13] removed tokens that are now part of islandora.tokens --- controlled_access_terms.tokens.inc | 76 ------------------------------ 1 file changed, 76 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index a29631c..b010523 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -49,27 +49,6 @@ function controlled_access_terms_token_info() { 'name' => t("PDF Url"), 'description' => t('URL to related media file if "Original file" is a PDF file'), ]; - $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], @@ -125,41 +104,6 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti 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': - $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) { - $file = controlled_access_terms_image_from_media($media); - if (!is_null($file) && !empty($file)) { - // url()); - $url = file_create_url($file->getFileUri()); - $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; } } } @@ -342,23 +286,3 @@ function controlled_access_terms_remove_comma($input, $remove_comma = FALSE) { 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(MediaInterface $media) { - $files = []; - if ($media->hasField('field_media_image')) { - $files = $media->get('field_media_image')->referencedEntities(); - } - return !empty($files) ? reset($files) : NULL; -} From 23145942ab2bd8adb265d448f30bc48b6e3b3b45 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Fri, 16 Jul 2021 22:25:14 +0000 Subject: [PATCH 09/13] localized the function names so they do not collide with code in islandora tokens file for #1171 --- controlled_access_terms.tokens.inc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index b010523..02d8d3a 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -78,31 +78,31 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti 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); + $replacements[$original] = controlled_access_terms_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); + $replacements[$original] = controlled_access_terms_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', ''); + $replacements[$original] = controlled_access_terms_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'); + $replacements[$original] = controlled_access_terms_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); + $replacements[$original] = controlled_access_terms_normalize_date_format($data['node'], 'field_edtf_date_created', $original); break; case 'pdf_url': - $replacements[$original] = ' ' . _url_to_service_file_media_by_mimetype($data['node'], 'application/pdf'); + $replacements[$original] = ' ' . controlled_access_terms_url_to_service_file_media_by_mimetype($data['node'], 'application/pdf'); break; } } @@ -126,7 +126,7 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti * @return string * The tokenized value for the given data. */ -function _get_term_with_rel_type($node, $field_name, $relation_type = '', $remove_comma = FALSE) { +function controlled_access_terms_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)) { @@ -172,7 +172,7 @@ function _get_term_with_rel_type($node, $field_name, $relation_type = '', $remov * * @throws Exception */ -function _normalize_date_format($node, $field_name, $original) { +function controlled_access_terms_normalize_date_format($node, $field_name, $original) { $retval = ''; try { $created_date = ($node->hasField($field_name) ? $node->get($field_name) : NULL); @@ -230,7 +230,7 @@ function _normalize_date_format($node, $field_name, $original) { * @return string * The tokenized value for the given data. */ -function _url_to_service_file_media_by_mimetype($node, $mime_type) { +function controlled_access_terms_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); From 087fffcfe5f1c6f3cf1b9469104fd9c928a9a642 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Tue, 20 Jul 2021 16:44:40 +0000 Subject: [PATCH 10/13] use the delimiter from the metatag patch if configured -- only flip-flop values when the delimiter is not a comma for #1171 --- controlled_access_terms.tokens.inc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index 02d8d3a..f337e88 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -127,6 +127,10 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti * The tokenized value for the given data. */ function controlled_access_terms_get_term_with_rel_type($node, $field_name, $relation_type = '', $remove_comma = FALSE) { + $settings = \Drupal::config('metatag.settings'); + $entity_type_groups = $settings->get('entity_type_groups'); + $separator = $settings->get('separator'); + $separator = ($separator) ? $separator : ','; $matches = []; $field = ($node->hasField($field_name) ? $node->get($field_name) : NULL); if (is_object($field)) { @@ -141,20 +145,20 @@ function controlled_access_terms_get_term_with_rel_type($node, $field_name, $rel if ($tid['rel_type'] == $relation_type) { $term = Term::load($tid['target_id']); if ($term) { - $matches[] = controlled_access_terms_remove_comma($term->getName(), $remove_comma); + $matches[] = ($separator == ',') ? controlled_access_terms_remove_comma($term->getName(), $remove_comma) : $term->getName(); } } } else { $term = Term::load($tid['target_id']); if ($term) { - $matches[] = controlled_access_terms_remove_comma($term->getName(), $remove_comma); + $matches[] = ($separator == ',') ? controlled_access_terms_remove_comma($term->getName(), $remove_comma) : $term->getName(); } } } } } - return implode(", ", $matches); + return implode($separator . " ", $matches); } /** From 87be45e4f5b6c99c3b706add8406e0252ae6b897 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Tue, 20 Jul 2021 18:42:59 +0000 Subject: [PATCH 11/13] cleanup variable that was not used and comments --- controlled_access_terms.tokens.inc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index f337e88..66e40a9 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -107,6 +107,11 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti } } } + // For the two contributors in my test case, their names are "Mouse, Mickey" and "Who, Doctor" yet + // while the $replacements looks correctly glued together using the pipe character "|": + // "Mouse, Mickey| Who, Doctor" + // the metatag is switching that back to a comma. + // return $replacements; } @@ -128,7 +133,6 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti */ function controlled_access_terms_get_term_with_rel_type($node, $field_name, $relation_type = '', $remove_comma = FALSE) { $settings = \Drupal::config('metatag.settings'); - $entity_type_groups = $settings->get('entity_type_groups'); $separator = $settings->get('separator'); $separator = ($separator) ? $separator : ','; $matches = []; @@ -158,7 +162,7 @@ function controlled_access_terms_get_term_with_rel_type($node, $field_name, $rel } } } - return implode($separator . " ", $matches); + return implode($separator, $matches); } /** From d7c4b36f26a5ef0092386f5877718aed7122d7d9 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Wed, 21 Jul 2021 17:13:22 +0000 Subject: [PATCH 12/13] removed the PDF URL token logic and moved to islandora tokens for #1171 --- controlled_access_terms.tokens.inc | 54 ------------------------------ 1 file changed, 54 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index 6d0dfc9..8345513 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -45,10 +45,6 @@ function controlled_access_terms_token_info() { 'name' => t("Publication date"), 'description' => t('Show the "Date Created" 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'), - ]; return [ 'types' => ['islandoratokens' => $type], @@ -100,10 +96,6 @@ function controlled_access_terms_tokens($type, $tokens, array $data, array $opti case 'publication_date': $replacements[$original] = controlled_access_terms_normalize_date_format($data['node'], 'field_edtf_date_created', $original); break; - - case 'pdf_url': - $replacements[$original] = ' ' . controlled_access_terms_url_to_service_file_media_by_mimetype($data['node'], 'application/pdf'); - break; } } } @@ -222,52 +214,6 @@ function controlled_access_terms_normalize_date_format($node, $field_name, $orig 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 controlled_access_terms_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(); - return $url; - } - } - } - } -} - /** * Conditionally removes any commas and reverse the order of the string values. * From 5aa8c3491451c0d7b71b4fc7267a682dc4c9e7b1 Mon Sep 17 00:00:00 2001 From: Willow Gillingham Date: Wed, 21 Jul 2021 17:16:30 +0000 Subject: [PATCH 13/13] removed unused references to Media and File classes --- controlled_access_terms.tokens.inc | 2 -- 1 file changed, 2 deletions(-) diff --git a/controlled_access_terms.tokens.inc b/controlled_access_terms.tokens.inc index 8345513..2e3e538 100644 --- a/controlled_access_terms.tokens.inc +++ b/controlled_access_terms.tokens.inc @@ -8,8 +8,6 @@ */ 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;