You are here

function shorten_tokens in Shorten URLs 8.2

Same name and namespace in other branches
  1. 8 shorten.module \shorten_tokens()
  2. 7.2 shorten.module \shorten_tokens()
  3. 7 shorten.module \shorten_tokens()

Implements hook_tokens().

File

./shorten.module, line 558

Code

function shorten_tokens($type, $tokens, array $data = [], array $options = []) {
  $replacements = [];
  if ($type == 'node' && !empty($data['node']) && isset($tokens['short-url'])) {
    $node = (object) $data['node'];

    // @FIXME
    // url() expects a route name or an external URI.
    $url = Url::fromUri('internal:/node' . $node->nid, [
      'absolute' => TRUE,
      'alias' => \Drupal::config('shorten.settings')
        ->get('shorten_use_alias'),
    ])
      ->toString();
    $replacements[$tokens['short-url']] = shorten_url($url);
  }

  // General URL token replacement.
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);

  // URL tokens.
  if ($type == 'url' && !empty($data['path'])) {
    $path = $data['path'];
    if (isset($data['options'])) {

      // Merge in the URL options if available.
      $url_options = $data['options'] + $url_options;
    }
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'shorten':

          // @FIXME
          // url() expects a route name or an external URI.
          $value = Url::fromUri('internal:/' . $path, $url_options)
            ->toString();
          $replacements[$original] = shorten_url($value);
          break;
      }
    }
  }
  return $replacements;
}