You are here

function token_custom_tokens in Custom Tokens 8

Same name and namespace in other branches
  1. 7.2 token_custom.module \token_custom_tokens()
  2. 7 token_custom.module \token_custom_tokens()

Implements hook_tokens().

File

./token_custom.module, line 44
Hooks for Custom Tokens module.

Code

function token_custom_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  $token_customs = TokenCustom::loadMultiple(array_keys($tokens));
  foreach ($token_customs as $machine_name => $token_custom) {
    if ($type == $token_custom
      ->bundle() && array_key_exists($machine_name, $tokens)) {
      if ($token_custom instanceof TranslatableInterface) {
        $langcode = isset($options['langcode']) ? $options['langcode'] : Language::LANGCODE_DEFAULT;
        $token_custom = \Drupal::service('entity.repository')
          ->getTranslationFromContext($token_custom, $langcode);
      }
      $replacements[$tokens[$machine_name]] = $token_custom
        ->getFormattedContent();
      $bubbleable_metadata
        ->addCacheableDependency($token_custom);
    }
  }
  return $replacements;
}