Skip to content
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

Define token for linked-agent #30

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions controlled_access_terms.module
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,55 @@ function controlled_access_terms_update_8002() {
$entity_view_display->save(TRUE);
}
}

/**
* Define token for Linked Agent by relator, e.g. [node:linked-agent:cre].
*
* Implements hook_token_info().
*/
function controlled_access_terms_token_info() {

$info['tokens']['node']['linked-agent'] = array(
'name' => t('Linked Agent:relator'),
'description' => t('Linked Agent by relator'),
);

return $info;
}

/**
* Implements hook_tokens().
*/
function controlled_access_terms_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];

if ($type == 'node' && !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) {
if (strpos($name, 'linked-agent') !== FALSE) {
$output = array();
// Break linked-agent:aaa to get relator abbreviation.
$token_parts = explode(':', $name);
if (isset($data['node']->field_linked_agent)) {
foreach ($data['node']->field_linked_agent as $linked_agent) {
if ($linked_agent->rel_type == 'relators:' . $token_parts[1]) {
$term = \Drupal\taxonomy\Entity\Term::load($linked_agent->target_id);
$output[] = $term->getName();
}
}
}
$replacements[$original] = implode(',', $output);
break;
}
}
}

return $replacements;
}