Skip to content
Snippets Groups Projects
Commit 4f3888a2 authored by Brian Canini's avatar Brian Canini
Browse files

new custom module

customtokens - adds custom token for research url alias
parent f04b9141
No related branches found
No related tags found
No related merge requests found
name: Custom Tokens
description: 'Creates custom tokens'
core: 8.x
type: module
package: ASC Tech
<?php
/**
* Implements hook_token_info().
*/
function customtokens_token_info() {
$type = [
'name' => t('Custom Token'),
'description' => t('Tokens for custom things.'),
];
$node['landingct'] = [
'name' => t("Research Landing Page"),
'description' => t('The node\'s Research Landing Page'),
];
return [
'types' => ['customtoken' => $type],
'tokens' => ['customtoken' => $node],
];
}
/**
* Implements hook_tokens().
*/
function customtokens_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
$nids = \Drupal::entityQuery('node')->condition('type','research_landing_page')->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$lid = array_shift(array_keys($nodes));
$alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$lid);
if ($type == 'customtoken' && !empty($data['node'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'landingct':
$replacements[$original] = $alias;
break;
}
}
}
return $replacements;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment