You are here

token_var.module in Token Variable 8

Same filename and directory in other branches
  1. 6 token_var.module
  2. 7 token_var.module

Contains module code.

File

token_var.module
View source
<?php

/**
 * @file
 * Contains module code.
 */
use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 */
function token_var_token_info() {
  $config = \Drupal::config('token_var.settings')
    ->get('token_var_replacements');
  $tokens = [];
  $type = array(
    'name' => t('Variables'),
    'description' => t('Tokens related to drupal variables (Only variables that contain strings can be used).'),
  );

  // Tokens.
  if (!empty($config)) {
    foreach ($config as $name => $values) {
      foreach ($values as $key => $value) {
        $tokens[$name . ':' . $key] = array(
          'name' => $name . ':' . $key,
          'description' => $name . ':' . $key,
        );
      }
    }
  }
  return array(
    'types' => array(
      'variables' => $type,
    ),
    'tokens' => array(
      'variables' => $tokens,
    ),
  );
}

/**
 * Implements hook_tokens().
 */
function token_var_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacement = array();
  if ($type == 'variables') {
    foreach ($tokens as $name => $original) {
      $name = explode(':', $name);
      $key = $name[1];
      $name = str_replace('|', '.', $name[0]);
      $replacement[$original] = \Drupal::config($name)
        ->get($key);
    }
  }
  return $replacement;
}

Functions

Namesort descending Description
token_var_tokens Implements hook_tokens().
token_var_token_info Implements hook_token_info().