Skip to content

Instantly share code, notes, and snippets.

@luksak
Created November 7, 2017 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luksak/6e1d24e10be056a304b46ce77e90b71f to your computer and use it in GitHub Desktop.
Save luksak/6e1d24e10be056a304b46ce77e90b71f to your computer and use it in GitHub Desktop.
This provides a taken to access the site's base URL. See: https://www.drupal.org/node/1088112
/**
* Implements hook_token_info_alter().
*/
function base_url_token_token_info_alter(&$data) {
if (isset($data['tokens']['site'])) {
$data['tokens']['site']['base-url'] = [
'name' => t("Base URL"),
'description' => t("The site's base URL"),
];
}
}
/**
* Implements hook_tokens().
*/
function base_url_token_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = array();
if ($type == 'site') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'base-url':
$base_url = \Drupal::request()->getSchemeAndHttpHost();
$replacements[$original] = $base_url;
break;
}
}
}
return $replacements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment