<?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; }