diff --git a/core/modules/language/js/language.switcher.js b/core/modules/language/js/language.switcher.js new file mode 100644 index 0000000..a7ed73a --- /dev/null +++ b/core/modules/language/js/language.switcher.js @@ -0,0 +1,24 @@ +/** + * @file + * Attaches behaviors for the Language switch block. + */ + +(function ($, Drupal, drupalSettings, _, Backbone, JSON, storage) { + + "use strict"; + + Drupal.behaviors.language_switcher = { + attach: function (context) { + var $context = $(context); + + // Set the destination parameter on each of the contextual links. + var destination = 'destination=' + Drupal.encodePath(drupalSettings.path.currentPath); + $context.find('.language-switcher a').each(function () { + var url = this.getAttribute('href'); + var glue = (url.indexOf('?') === -1) ? '?' : '&'; + this.setAttribute('href', url + glue + destination); + }); + } + } + +})(jQuery, Drupal, drupalSettings, _, Backbone, window.JSON, window.sessionStorage); diff --git a/core/modules/language/language.routing.yml b/core/modules/language/language.routing.yml index 4d78d6d..a9c0c16 100644 --- a/core/modules/language/language.routing.yml +++ b/core/modules/language/language.routing.yml @@ -85,3 +85,10 @@ language.content_settings_page: _form: 'Drupal\language\Form\ContentLanguageSettingsForm' requirements: _permission: 'administer languages' + +language.redirect: + path: '/language-redirect' + defaults: + _controller: '\Drupal\language\Controller\LanguageRedirectController::redirect' + requirements: + _access: 'TRUE' diff --git a/core/modules/language/src/Controller/LanguageRedirectController.php b/core/modules/language/src/Controller/LanguageRedirectController.php new file mode 100644 index 0000000..f961cb6 --- /dev/null +++ b/core/modules/language/src/Controller/LanguageRedirectController.php @@ -0,0 +1,28 @@ +query->get('destination'); + if (!isset($destination)) { + $destination = ""; + } + return new RedirectResponse($destination, 301); + } + + +} diff --git a/core/modules/language/src/Plugin/Block/LanguageBlock.php b/core/modules/language/src/Plugin/Block/LanguageBlock.php index 2e5a3cc..dc7dc63 100644 --- a/core/modules/language/src/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/src/Plugin/Block/LanguageBlock.php @@ -8,6 +8,7 @@ namespace Drupal\language\Plugin\Block; use Drupal\Core\Block\BlockBase; +use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -88,26 +89,30 @@ public function build() { $build = array(); if ($this->currentUser->isAnonymous()) { $path = drupal_is_front_page() ? '' : current_path(); - $type = $this->getDerivativeId(); - $links = $this->languageManager->getLanguageSwitchLinks($type, $path); - - if (isset($links->links)) { - $build = array( - '#theme' => 'links__language_block', - '#links' => $links->links, - '#attributes' => array( - 'class' => array( - "language-switcher-{$links->method_id}", - ), - ), - '#set_active_class' => TRUE, - ); - } } else { - // @todo Build the globally cacheable variant that depends on a redirect - // plus JavaScript. - // @see getRequiredCacheContexts() + $path = NULL; + $build['#attached'] = array( + 'js' => array( + drupal_get_path('module', 'language') . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'language.switcher.js', + ), + ); + } + $type = $this->getDerivativeId(); + $links = $this->languageManager->getLanguageSwitchLinks($type, $path); + + if (isset($links->links)) { + $build += array( + '#theme' => 'links__language_block', + '#links' => $links->links, + '#attributes' => array( + 'class' => array( + "language-switcher-{$links->method_id}", + "language-switcher" + ), + ), + '#set_active_class' => TRUE, + ); } return $build; } diff --git a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php index dc5617d..da5e926 100644 --- a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php +++ b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php @@ -143,6 +143,11 @@ function getLanguageSwitchLinks(Request $request, $type, $path) { else { $links[$langcode]['attributes']['class'][] = 'session-active'; } + + // No path given, add it later by JS. + if (!isset($path)) { + $links[$language->id]['route_name'] = 'language.redirect'; + } } return $links; diff --git a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php index be9f915..6879395 100644 --- a/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php +++ b/core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php @@ -197,6 +197,11 @@ function getLanguageSwitchLinks(Request $request, $type, $path) { 'language' => $language, 'attributes' => array('class' => array('language-link')), ); + + // No path given, add it later by JS. + if (!isset($path)) { + $links[$language->id]['route_name'] = 'language.redirect'; + } } return $links;