Created
November 7, 2017 13:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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