You are here

function matomo_tokens in Matomo Analytics 8

Same name and namespace in other branches
  1. 7.2 matomo.tokens.inc \matomo_tokens()

Implements hook_tokens().

File

./matomo.tokens.inc, line 34
Builds placeholder replacement tokens for user-related data.

Code

function matomo_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $sanitize = !empty($options['sanitize']);
  $replacements = [];
  if ($type == 'user' && !empty($data['user'])) {
    $account = $data['user'];
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Basic user account information.
        case 'matomo-role-names':
          $names = implode(',', $account
            ->getRoles());
          $replacements[$original] = $sanitize ? Html::escape($names) : $names;
          $bubbleable_metadata
            ->addCacheableDependency($account);
          break;
        case 'matomo-role-ids':
          $ids = implode(',', array_keys($account
            ->getRoles()));
          $replacements[$original] = $sanitize ? Html::escape($ids) : $ids;
          $bubbleable_metadata
            ->addCacheableDependency($account);
          break;
      }
    }
  }
  return $replacements;
}