function shorten_tokens in Shorten URLs 7
Same name and namespace in other branches
- 8.2 shorten.module \shorten_tokens()
- 8 shorten.module \shorten_tokens()
- 7.2 shorten.module \shorten_tokens()
Implements hook_tokens().
File
- ./
shorten.module, line 700 - 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'];
$replacements[$tokens['short-url']] = shorten_url(url('node/' . $node->nid, array(
'absolute' => TRUE,
'alias' => variable_get('shorten_use_alias', 1),
)));
}
// 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':
$value = url($path, $url_options);
$replacements[$original] = shorten_url($value);
break;
}
}
}
return $replacements;
}