6

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 2

17

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(),
  ];

  ...
1
3

Use Drupal.url()

ref: docroot/core/misc/drupal.js

1
  • 1
    It makes sense to make use of the core function, thanks !
    – echo
    Mar 15, 2018 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.