Commit ddc0b0b8 authored by alexpott's avatar alexpott

Issue #2218313 by jessebeach, dawehner: Toolbar subtree is broken if you use a different language.

parent 10b03890
...@@ -248,7 +248,8 @@ ...@@ -248,7 +248,8 @@
// (3) The orientation of the tray is vertical. // (3) The orientation of the tray is vertical.
if (!this.model.get('areSubtreesLoaded') && $activeTab.data('drupal-subtrees') !== undefined && orientation === 'vertical') { if (!this.model.get('areSubtreesLoaded') && $activeTab.data('drupal-subtrees') !== undefined && orientation === 'vertical') {
var subtreesHash = drupalSettings.toolbar.subtreesHash; var subtreesHash = drupalSettings.toolbar.subtreesHash;
var endpoint = Drupal.url('toolbar/subtrees/' + subtreesHash); var langcode = drupalSettings.toolbar.langcode;
var endpoint = Drupal.url('toolbar/subtrees/' + subtreesHash + '/' + langcode);
var cachedSubtreesHash = localStorage.getItem('Drupal.toolbar.subtreesHash'); var cachedSubtreesHash = localStorage.getItem('Drupal.toolbar.subtreesHash');
var cachedSubtrees = JSON.parse(localStorage.getItem('Drupal.toolbar.subtrees')); var cachedSubtrees = JSON.parse(localStorage.getItem('Drupal.toolbar.subtrees'));
var isVertical = this.model.get('orientation') === 'vertical'; var isVertical = this.model.get('orientation') === 'vertical';
......
...@@ -32,10 +32,19 @@ public function subtreesJsonp() { ...@@ -32,10 +32,19 @@ public function subtreesJsonp() {
/** /**
* Checks access for the subtree controller. * Checks access for the subtree controller.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
* @param string $langcode
* The langcode of the requested site, NULL if none given.
*
* @return string
* Returns AccessInterface::ALLOW when access was granted, otherwise
* AccessInterface::DENY.
*/ */
public function checkSubTreeAccess(Request $request) { public function checkSubTreeAccess(Request $request, $langcode) {
$hash = $request->get('hash'); $hash = $request->get('hash');
return ($this->currentUser()->hasPermission('access toolbar') && ($hash == _toolbar_get_subtrees_hash())) ? AccessInterface::ALLOW : AccessInterface::DENY; return ($this->currentUser()->hasPermission('access toolbar') && ($hash == _toolbar_get_subtrees_hash($langcode))) ? AccessInterface::ALLOW : AccessInterface::DENY;
} }
} }
...@@ -452,6 +452,55 @@ function testSubtreesJsonRequest() { ...@@ -452,6 +452,55 @@ function testSubtreesJsonRequest() {
$this->drupalGetJSON('toolbar/subtrees/' . $subtrees_hash); $this->drupalGetJSON('toolbar/subtrees/' . $subtrees_hash);
$this->assertResponse('200'); $this->assertResponse('200');
// Test that the subtrees hash changes with a different language code and
// that JSON is returned when a language code is specified.
// Create a new language with the langcode 'xx'.
$langcode = 'xx';
// The English name for the language. This will be translated.
$name = $this->randomName(16);
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'name' => $name,
'direction' => '0',
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
// Get a page with the new language langcode in the URL.
$this->drupalGet('/xx/test-page');
// Request a new page to refresh the drupalSettings object.
$subtrees_hash = $this->getSubtreesHash();
$this->drupalGetJSON('toolbar/subtrees/' . $subtrees_hash . '/' . $langcode);
$this->assertResponse('200');
}
/**
* Test that subtrees hashes vary by the language of the page.
*/
function testLanguageSwitching() {
// Create a new language with the langcode 'xx'.
$langcode = 'xx';
// The English name for the language. This will be translated.
$name = $this->randomName(16);
$edit = array(
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'name' => $name,
'direction' => '0',
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
// Get a page with the new language langcode in the URL.
$this->drupalGet('/xx/test-page');
// Assert different hash.
$new_subtree_hash = $this->getSubtreesHash();
// Assert that the old admin menu subtree hash and the new admin menu
// subtree hash are different.
$this->assertTrue($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this->assertNotEqual($this->hash, $new_subtree_hash, 'The user-specific subtree menu hash has been updated.');
} }
/** /**
......
...@@ -378,10 +378,12 @@ function toolbar_toolbar() { ...@@ -378,10 +378,12 @@ function toolbar_toolbar() {
// toolbar_subtrees route. We provide the JavaScript requesting that JSONP // toolbar_subtrees route. We provide the JavaScript requesting that JSONP
// script here with the hash parameter that is needed for that route. // script here with the hash parameter that is needed for that route.
// @see toolbar_subtrees_jsonp() // @see toolbar_subtrees_jsonp()
$langcode = \Drupal::languageManager()->getCurrentLanguage()->id;
$menu['toolbar_administration']['#attached']['js'][] = array( $menu['toolbar_administration']['#attached']['js'][] = array(
'type' => 'setting', 'type' => 'setting',
'data' => array('toolbar' => array( 'data' => array('toolbar' => array(
'subtreesHash' => _toolbar_get_subtrees_hash(), 'subtreesHash' => _toolbar_get_subtrees_hash($langcode),
'langcode' => $langcode,
)), )),
); );
...@@ -503,12 +505,15 @@ function toolbar_get_rendered_subtrees() { ...@@ -503,12 +505,15 @@ function toolbar_get_rendered_subtrees() {
/** /**
* Returns the hash of the per-user rendered toolbar subtrees. * Returns the hash of the per-user rendered toolbar subtrees.
* *
* @param string $langcode
* The langcode of the current request.
*
* @return string * @return string
* The hash of the admin_menu subtrees. * The hash of the admin_menu subtrees.
*/ */
function _toolbar_get_subtrees_hash() { function _toolbar_get_subtrees_hash($langcode) {
$uid = \Drupal::currentUser()->id(); $uid = \Drupal::currentUser()->id();
$cid = _toolbar_get_user_cid($uid); $cid = _toolbar_get_user_cid($uid, $langcode);
if ($cache = \Drupal::cache('toolbar')->get($cid)) { if ($cache = \Drupal::cache('toolbar')->get($cid)) {
$hash = $cache->data; $hash = $cache->data;
} }
...@@ -566,12 +571,14 @@ function toolbar_user_role_update(RoleInterface $role) { ...@@ -566,12 +571,14 @@ function toolbar_user_role_update(RoleInterface $role) {
* *
* @param int $uid * @param int $uid
* A user ID. * A user ID.
* @param string $langcode
* The langcode of the current request.
* *
* @return string * @return string
* A unique cache ID for the user. * A unique cache ID for the user.
*/ */
function _toolbar_get_user_cid($uid) { function _toolbar_get_user_cid($uid, $langcode) {
return 'toolbar_' . $uid . ':' . \Drupal::languageManager()->getCurrentLanguage()->id; return 'toolbar_' . $uid . ':' . $langcode;
} }
/** /**
......
toolbar.subtrees: toolbar.subtrees:
path: '/toolbar/subtrees/{hash}' path: '/toolbar/subtrees/{hash}/{langcode}'
defaults: defaults:
_controller: '\Drupal\toolbar\Controller\ToolbarController::subtreesJsonp' _controller: '\Drupal\toolbar\Controller\ToolbarController::subtreesJsonp'
langcode: null
requirements: requirements:
_custom_access: '\Drupal\toolbar\Controller\ToolbarController::checkSubTreeAccess' _custom_access: '\Drupal\toolbar\Controller\ToolbarController::checkSubTreeAccess'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment