function shorten_tokens in Shorten URLs 8
Same name and namespace in other branches
- 8.2 shorten.module \shorten_tokens()
- 7.2 shorten.module \shorten_tokens()
- 7 shorten.module \shorten_tokens()
Implements hook_tokens().
File
- ./
shorten.module, line 499 - Shortens URLs via external services.
Code
function shorten_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
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, array(
'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 = array(
'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;
}