You are here

function uc_store_tokens in Ubercart 8.4

Same name and namespace in other branches
  1. 7.3 uc_store/uc_store.tokens.inc \uc_store_tokens()

Implements hook_tokens().

File

uc_store/uc_store.tokens.inc, line 72
Token hooks for the uc_store module.

Code

function uc_store_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();
  $replacements = [];
  if ($type == 'site') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'login-link':
          $login_link = Url::fromRoute('user.page', [], [
            'absolute' => TRUE,
          ])
            ->toString();
          $replacements[$original] = Link::fromTextAndUrl($login_link, Url::fromUri($login_link))
            ->toString();
          break;
        case 'logo':

          // Use a logo; but only if we have one to use.
          $replacements[$original] = '';
          if ($uri = theme_get_setting('logo.url')) {
            $logo = [
              '#theme' => 'image',
              '#uri' => $uri,
            ];

            // Redefine $logo because '#theme' => 'image' always generates a
            // relative URL for local files. @todo remove this when this is
            // fixed by https://www.drupal.org/project/drupal/issues/2704597
            $logo = [
              '#markup' => '<img src="' . Url::fromUserInput($uri, [
                'absolute' => TRUE,
              ])
                ->toString() . '" typeof="foaf:Image" />',
            ];
            $replacements[$original] = drupal_render($logo);
          }
          break;
      }
    }
  }
  if ($type == 'store') {
    $config = \Drupal::config('uc_store.settings');
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = uc_store_name();
          break;
        case 'link':
          $replacements[$original] = Link::createFromRoute(uc_store_name(), '<front>', [], [
            'absolute' => TRUE,
          ])
            ->toString();
          break;
        case 'email':
          $replacements[$original] = uc_store_email();
          break;
        case 'phone':
          $replacements[$original] = $config
            ->get('phone');
          break;
        case 'fax':
          $replacements[$original] = $config
            ->get('fax');
          break;
        case 'address':

          // Cast Address object to string to get country-specific formatting.
          $address = [
            '#markup' => (string) uc_store_address(),
          ];
          $replacements[$original] = drupal_render($address);
          break;
        case 'help-url':
          $replacements[$original] = Url::fromUserInput('/' . $config
            ->get('help_page'), [
            'absolute' => TRUE,
          ])
            ->toString();
          break;
      }
    }

    // Handle chaining for tokens that have 'type' defined in hook_token_info()
    if ($link_tokens = $token_service
      ->findWithPrefix($tokens, 'help-url')) {
      $replacements += $token_service
        ->generate('url', $link_tokens, [
        'path' => $config
          ->get('help_page'),
      ], $options, $bubbleable_metadata);
    }
  }
  return $replacements;
}