You are here

function google_analytics_tokens in Google Analytics 4.x

Same name and namespace in other branches
  1. 8.3 google_analytics.tokens.inc \google_analytics_tokens()
  2. 8.2 google_analytics.tokens.inc \google_analytics_tokens()

Implements hook_tokens().

File

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

Code

function google_analytics_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 'role-names':
          $names = implode(',', $account
            ->getRoles());
          $replacements[$original] = $sanitize ? Html::escape($names) : $names;
          $bubbleable_metadata
            ->addCacheableDependency($account);
          break;
        case 'role-ids':
          $ids = implode(',', array_keys($account
            ->getRoles()));
          $replacements[$original] = $sanitize ? Html::escape($ids) : $ids;
          $bubbleable_metadata
            ->addCacheableDependency($account);
          break;
      }
    }
  }
  return $replacements;
}