I'm trying to get the site URL in JavaScript. In Drupal 7, this is stored in Drupal.settings.basePath
. What's the Drupal 8 equivalent?
2 Answers
In Drupal 8 this setting is:
drupalSettings.path.baseUrl
You find more in core/module/system/system.module
:
function system_js_settings_alter(&$settings, AttachedAssetsInterface $assets) {
...
$path_settings = [
'baseUrl' => $request->getBaseUrl() . '/',
'pathPrefix' => $pathPrefix,
'currentPath' => $current_path,
'currentPathIsAdmin' => $current_path_is_admin,
'isFront' => \Drupal::service('path.matcher')->isFrontPage(),
'currentLanguage' => \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId(),
];
...
-
2Perfect, just have to make sure the dependency is declared
- core/drupalSettings
drupal.org/theme-guide/8/assets Commented May 25, 2016 at 18:00
Use Drupal.url()
ref: docroot/core/misc/drupal.js
-
1