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

8.0.x
Alex Pott 2014-04-03 16:13:13 +01:00
parent 10b0389066
commit ddc0b0b878
5 changed files with 76 additions and 9 deletions

View File

@ -248,7 +248,8 @@
// (3) The orientation of the tray is vertical.
if (!this.model.get('areSubtreesLoaded') && $activeTab.data('drupal-subtrees') !== undefined && orientation === 'vertical') {
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 cachedSubtrees = JSON.parse(localStorage.getItem('Drupal.toolbar.subtrees'));
var isVertical = this.model.get('orientation') === 'vertical';

View File

@ -32,10 +32,19 @@ class ToolbarController extends ControllerBase {
/**
* 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');
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;
}
}

View File

@ -452,6 +452,55 @@ class ToolbarAdminMenuTest extends WebTestBase {
$this->drupalGetJSON('toolbar/subtrees/' . $subtrees_hash);
$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.');
}
/**

View File

@ -378,10 +378,12 @@ function toolbar_toolbar() {
// toolbar_subtrees route. We provide the JavaScript requesting that JSONP
// script here with the hash parameter that is needed for that route.
// @see toolbar_subtrees_jsonp()
$langcode = \Drupal::languageManager()->getCurrentLanguage()->id;
$menu['toolbar_administration']['#attached']['js'][] = array(
'type' => 'setting',
'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() {
/**
* Returns the hash of the per-user rendered toolbar subtrees.
*
* @param string $langcode
* The langcode of the current request.
*
* @return string
* The hash of the admin_menu subtrees.
*/
function _toolbar_get_subtrees_hash() {
function _toolbar_get_subtrees_hash($langcode) {
$uid = \Drupal::currentUser()->id();
$cid = _toolbar_get_user_cid($uid);
$cid = _toolbar_get_user_cid($uid, $langcode);
if ($cache = \Drupal::cache('toolbar')->get($cid)) {
$hash = $cache->data;
}
@ -566,12 +571,14 @@ function toolbar_user_role_update(RoleInterface $role) {
*
* @param int $uid
* A user ID.
* @param string $langcode
* The langcode of the current request.
*
* @return string
* A unique cache ID for the user.
*/
function _toolbar_get_user_cid($uid) {
return 'toolbar_' . $uid . ':' . \Drupal::languageManager()->getCurrentLanguage()->id;
function _toolbar_get_user_cid($uid, $langcode) {
return 'toolbar_' . $uid . ':' . $langcode;
}
/**

View File

@ -1,6 +1,7 @@
toolbar.subtrees:
path: '/toolbar/subtrees/{hash}'
path: '/toolbar/subtrees/{hash}/{langcode}'
defaults:
_controller: '\Drupal\toolbar\Controller\ToolbarController::subtreesJsonp'
langcode: null
requirements:
_custom_access: '\Drupal\toolbar\Controller\ToolbarController::checkSubTreeAccess'