function relation_tokens in Relation 8.2
Same name and namespace in other branches
- 8 relation.tokens.inc \relation_tokens()
- 7 relation.tokens.inc \relation_tokens()
Implements hook_tokens().
File
- ./
relation.tokens.inc, line 76 - Builds placeholder replacement tokens for relation data.
Code
function relation_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array(
'absolute' => TRUE,
);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'relation' && !empty($data['relation'])) {
$relation = $data['relation'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the relation.
case 'relation_id':
$replacements[$original] = $relation
->id();
break;
case 'revision_id':
$replacements[$original] = $relation
->getRevisionId();
break;
case 'relation_type':
$replacements[$original] = $sanitize ? check_plain($relation->relation_type) : $relation->type;
break;
case 'type-name':
$type_label = $relation->relation_type->entity
->label();
$replacements[$original] = $sanitize ? check_plain($type_label) : $type_label;
break;
case 'url':
$replacements[$original] = url('relation/' . $relation
->id(), $url_options);
break;
case 'edit-url':
$replacements[$original] = url('relation/' . $relation
->id() . '/edit', $url_options);
break;
// Default values for the chained tokens handled below.
case 'author':
if ($relation->uid == 0) {
$name = variable_get('anonymous', t('Anonymous'));
}
else {
$user = User::load($relation->uid);
$name = $user->name;
}
$replacements[$original] = $sanitize ? filter_xss($name) : $name;
break;
case 'created':
$replacements[$original] = \Drupal::service('date.formatter')
->format($relation->created, 'medium', '', NULL, $language_code);
break;
case 'changed':
$replacements[$original] = \Drupal::service('date.formatter')
->format($relation->changed, 'medium', '', NULL, $language_code);
break;
}
}
if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
$author = User::load($relation->uid);
$replacements += token_generate('user', $author_tokens, array(
'user' => $author,
), $options);
}
if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
$replacements += token_generate('date', $created_tokens, array(
'date' => $relation->created,
), $options);
}
if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
$replacements += token_generate('date', $changed_tokens, array(
'date' => $relation->changed,
), $options);
}
}
return $replacements;
}