You are here

function opigno_messaging_tokens in Opigno messaging 8

Implements hook_tokens().

File

./opigno_messaging.tokens.inc, line 37
Builds placeholder replacement tokens opigno-wide data.

Code

function opigno_messaging_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = NULL;
  }
  $replacements = [];
  if ($type == 'opigno') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'url-with-slash':

          /** @var \Drupal\Core\GeneratedUrl $result */
          $result = Url::fromRoute('<front>', [], $url_options)
            ->toString(TRUE);
          $bubbleable_metadata
            ->addCacheableDependency($result);
          $replacements[$original] = $result
            ->getGeneratedUrl();

          // When multilingual functional enabled site url returns without slash.
          if (substr($replacements[$original], -1) != '/') {
            $replacements[$original] .= '/';
          }
          break;
      }
    }
  }
  return $replacements;
}