Skip to content

Instantly share code, notes, and snippets.

@seth-shaw-unlv
Created May 20, 2022 20:10
Show Gist options
  • Select an option

  • Save seth-shaw-unlv/cdd3c31efe128b241f973a0467faf901 to your computer and use it in GitHub Desktop.

Select an option

Save seth-shaw-unlv/cdd3c31efe128b241f973a0467faf901 to your computer and use it in GitHub Desktop.
Setting up ARKs as aliases
<?php
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function islandora_local_token_info() {
$type = [
'name' => t('Local'),
'description' => t('Locally defined tokens.'),
'needs-data' => 'node',
];
$node['dc-ark'] = [
'name' => t("DC ARK"),
'description' => t("The ARK from the field_archival_resource_key field."),
];
return [
'types' => ['node' => $type],
'tokens' => ['node' => $node],
];
}
/**
* Implements hook_tokens().
*/
function islandora_local_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$ark_field_id = 'field_archival_resource_key';
if ($type == 'node' && !empty($data['node'])) {
/** @var \Drupal\node\NodeInterface $node */
$node = $data['node'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the node.
case 'dc-ark':
$type = $node->getType();
$matches = [];
if (isset($node->get($ark_field_id)->uri) && preg_match('#ark:/\d+/.*#i', $node->get($ark_field_id)->uri, $matches)) {
$replacements[$original] = $matches[0];
}
else {
$replacements[$original] = "/node/{$node->id()}";
}
break;
}
}
}
return $replacements;
}
langcode: en
status: true
dependencies:
module:
- node
id: dc_ark
label: 'DC Object ARK'
type: 'canonical_entities:node'
pattern: '[node:dc-ark]'
selection_criteria:
886b9506-4a92-4437-81c6-a7600ff02fb2:
id: node_type
negate: false
uuid: 886b9506-4a92-4437-81c6-a7600ff02fb2
context_mapping:
node: node
bundles:
dc_object: dc_object
selection_logic: and
weight: -5
relationships: { }
enabled_entity_types:
- user
punctuation:
double_quotes: 0
quotes: 0
backtick: 0
comma: 0
period: 0
hyphen: 1
underscore: 0
colon: 0
semicolon: 0
pipe: 0
left_curly: 0
left_square: 0
right_curly: 0
right_square: 0
plus: 0
equal: 0
asterisk: 0
ampersand: 0
percent: 0
caret: 0
dollar: 0
hash: 0
at: 0
exclamation: 0
tilde: 0
left_parenthesis: 0
right_parenthesis: 0
question_mark: 0
less_than: 0
greater_than: 0
slash: 0
back_slash: 0
verbose: false
separator: '-'
max_length: 100
max_component_length: 100
transliterate: true
reduce_ascii: false
case: true
ignore_words: 'a, an, as, at, before, but, by, for, from, is, in, into, like, of, off, on, onto, per, since, than, the, this, that, to, up, via, with'
update_action: 2
safe_tokens:
- alias
- path
- join-path
- login-url
- url
- url-brief
- dc-ark
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment