Skip to content
Snippets Groups Projects
Commit ebeda109 authored by Patrick Fey's avatar Patrick Fey
Browse files

Issue #3458373 by FeyP, lelkneralfaro, dshields: [10.3 Regression] Menu...

Issue #3458373 by FeyP, lelkneralfaro, dshields: [10.3 Regression] Menu containing language switcher link non-editable
parent e2d09a24
Branches
Tags
1 merge request!5Resolve #3458373 "Menu containing language"
Pipeline #229881 passed with warnings
...@@ -4,7 +4,7 @@ description: "Allows you to add language switcher links to a menu." ...@@ -4,7 +4,7 @@ description: "Allows you to add language switcher links to a menu."
configure: language_switcher_menu.configure configure: language_switcher_menu.configure
type: module type: module
core_version_requirement: ^9 || ^10 core_version_requirement: ^10.1
php: 7.4 php: 7.4
dependencies: dependencies:
......
...@@ -5,4 +5,5 @@ services: ...@@ -5,4 +5,5 @@ services:
- '@access_manager' - '@access_manager'
- '@current_user' - '@current_user'
- '@entity_type.manager' - '@entity_type.manager'
- '@module_handler'
- '@path.validator' - '@path.validator'
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\language_switcher_menu\Form; namespace Drupal\language_switcher_menu\Form;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
...@@ -49,6 +50,8 @@ class SettingsForm extends ConfigFormBase { ...@@ -49,6 +50,8 @@ class SettingsForm extends ConfigFormBase {
* *
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects. * The factory for configuration objects.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
* The typed config manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager. * The entity type manager.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
...@@ -58,8 +61,8 @@ class SettingsForm extends ConfigFormBase { ...@@ -58,8 +61,8 @@ class SettingsForm extends ConfigFormBase {
* @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_form_selector * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_form_selector
* The menu parent form selector. * The menu parent form selector.
*/ */
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_form_selector) { public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_form_selector) {
parent::__construct($config_factory); parent::__construct($config_factory, $typed_config_manager);
$this->entityTypeManager = $entity_type_manager; $this->entityTypeManager = $entity_type_manager;
$this->languageManager = $language_manager; $this->languageManager = $language_manager;
$this->menuLinkManager = $menu_link_manager; $this->menuLinkManager = $menu_link_manager;
...@@ -74,6 +77,7 @@ class SettingsForm extends ConfigFormBase { ...@@ -74,6 +77,7 @@ class SettingsForm extends ConfigFormBase {
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('config.factory'), $container->get('config.factory'),
$container->get('config.typed'),
$container->get('entity_type.manager'), $container->get('entity_type.manager'),
$container->get('language_manager'), $container->get('language_manager'),
$container->get('plugin.manager.menu.link'), $container->get('plugin.manager.menu.link'),
......
...@@ -5,6 +5,7 @@ namespace Drupal\language_switcher_menu; ...@@ -5,6 +5,7 @@ namespace Drupal\language_switcher_menu;
use Drupal\Core\Access\AccessManagerInterface; use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Menu\DefaultMenuLinkTreeManipulators; use Drupal\Core\Menu\DefaultMenuLinkTreeManipulators;
use Drupal\Core\Menu\MenuLinkInterface; use Drupal\Core\Menu\MenuLinkInterface;
use Drupal\Core\Path\PathValidatorInterface; use Drupal\Core\Path\PathValidatorInterface;
...@@ -35,11 +36,13 @@ class LanguageLinkAccessMenuTreeManipulator extends DefaultMenuLinkTreeManipulat ...@@ -35,11 +36,13 @@ class LanguageLinkAccessMenuTreeManipulator extends DefaultMenuLinkTreeManipulat
* The current user. * The current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager. * The entity type manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
* The path validator. * The path validator.
*/ */
public function __construct(AccessManagerInterface $access_manager, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, PathValidatorInterface $path_validator) { public function __construct(AccessManagerInterface $access_manager, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, PathValidatorInterface $path_validator) {
parent::__construct($access_manager, $account, $entity_type_manager); parent::__construct($access_manager, $account, $entity_type_manager, $module_handler);
$this->pathValidator = $path_validator; $this->pathValidator = $path_validator;
} }
......
...@@ -173,7 +173,7 @@ class LanguageSwitcherLink extends MenuLinkDefault { ...@@ -173,7 +173,7 @@ class LanguageSwitcherLink extends MenuLinkDefault {
*/ */
public function getRouteName() { public function getRouteName() {
$link = $this->getLink(); $link = $this->getLink();
return isset($link['url']) ? $link['url']->getRouteName() : ''; return isset($link['url']) ? $link['url']->getRouteName() : '<nolink>';
} }
/** /**
......
name: "Test Language Switcher Menu"
description: "Support testing of Language Switcher Menu."
type: module
package: Testing
core_version_requirement: ^10.1
<?php
/**
* @file
* Hook implementations by Test Language Switcher Menu module.
*/
use Drupal\Core\Url;
/**
* Implements hook_language_switch_links_alter().
*/
function test_language_switcher_menu_language_switch_links_alter(array &$links, $type, Url $url) {
if (!\Drupal::state()->get('test_language_switcher_menu.remove_current_language_switch_link')) {
return;
}
// Unset language switch link for current language.
$current_language = \Drupal::languageManager()->getCurrentLanguage()->getId();
unset($links[$current_language]);
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\language_switcher_menu\Functional; namespace Drupal\Tests\language_switcher_menu\Functional;
use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
...@@ -24,6 +25,7 @@ class LanguageSwitcherMenuTest extends BrowserTestBase { ...@@ -24,6 +25,7 @@ class LanguageSwitcherMenuTest extends BrowserTestBase {
'menu_ui', 'menu_ui',
'language_switcher_menu', 'language_switcher_menu',
'menu_test', 'menu_test',
'test_language_switcher_menu',
]; ];
/** /**
...@@ -58,6 +60,7 @@ class LanguageSwitcherMenuTest extends BrowserTestBase { ...@@ -58,6 +60,7 @@ class LanguageSwitcherMenuTest extends BrowserTestBase {
]); ]);
$this->users['admin_menu_regular'] = $this->drupalCreateUser([ $this->users['admin_menu_regular'] = $this->drupalCreateUser([
'administer menu', 'administer menu',
'view language_switcher_menu links',
]); ]);
$this->users['access_content'] = $this->drupalCreateUser([ $this->users['access_content'] = $this->drupalCreateUser([
'access content', 'access content',
...@@ -65,6 +68,9 @@ class LanguageSwitcherMenuTest extends BrowserTestBase { ...@@ -65,6 +68,9 @@ class LanguageSwitcherMenuTest extends BrowserTestBase {
$this->users['view_links'] = $this->drupalCreateUser([ $this->users['view_links'] = $this->drupalCreateUser([
'view language_switcher_menu links', 'view language_switcher_menu links',
]); ]);
// Initialize state for test language switcher module.
\Drupal::state()->set('test_language_switcher_menu.remove_current_language_switch_link', FALSE);
} }
/** /**
...@@ -182,6 +188,42 @@ class LanguageSwitcherMenuTest extends BrowserTestBase { ...@@ -182,6 +188,42 @@ class LanguageSwitcherMenuTest extends BrowserTestBase {
'/fr' => 'Home', '/fr' => 'Home',
], 'The menu links are correct in a non-standard language.'); ], 'The menu links are correct in a non-standard language.');
// Assert that main menu management page doesn't show a white page.
$this->drupalLogin($this->users['admin_menu_regular']);
$this->drupalGet('admin/structure/menu/manage/main');
$this->assertSession()->statusCodeEquals(200);
// Assert that menu switch links are visible.
$this->assertMenuLinks([
'/admin/structure/menu/manage/main' => 'English',
'/fr/admin/structure/menu/manage/main' => 'French',
'/' => 'Home',
], 'Both language links are shown.');
// Enable removal of current language switch link in test module.
\Drupal::state()->set('test_language_switcher_menu.remove_current_language_switch_link', TRUE);
// Assert that main menu management page still doesn't show a white page.
$this->drupalGet('admin/structure/menu/manage/main');
$this->assertSession()->statusCodeEquals(200);
// Assert that menu switch link in current language has been removed.
$this->assertMenuLinks([
'/fr/admin/structure/menu/manage/main' => 'French',
'/' => 'Home',
], 'The language link for the current language is not shown.');
// Disable removal of current language switch link in test module.
\Drupal::state()->set('test_language_switcher_menu.remove_current_language_switch_link', FALSE);
// Assert that menu switch links are visible.
$this->drupalGet('admin/structure/menu/manage/main');
$this->assertMenuLinks([
'/admin/structure/menu/manage/main' => 'English',
'/fr/admin/structure/menu/manage/main' => 'French',
'/' => 'Home',
], 'Both language links are visible in the menu.');
// Reconfigure to disable. // Reconfigure to disable.
$this->drupalLogin($this->users['admin_user']); $this->drupalLogin($this->users['admin_user']);
$edit = [ $edit = [
...@@ -229,17 +271,31 @@ class LanguageSwitcherMenuTest extends BrowserTestBase { ...@@ -229,17 +271,31 @@ class LanguageSwitcherMenuTest extends BrowserTestBase {
$this->assertSession()->pageTextNotContains('Error message'); $this->assertSession()->pageTextNotContains('Error message');
$this->assertSession()->pageTextNotContains("The path '<current>' is inaccessible."); $this->assertSession()->pageTextNotContains("The path '<current>' is inaccessible.");
$this->assertSession()->pageTextContains('The menu link has been saved.'); $this->assertSession()->pageTextContains('The menu link has been saved.');
$this->assertMenuLinks([ DeprecationHelper::backwardsCompatibleCall(
'/' => 'Home', currentVersion: \Drupal::VERSION,
'/admin/structure/menu/item/1/edit' => 'link_current', deprecatedVersion: '10.3',
], 'Custom link targeting <current> is visible in menu with permission to link to any page.'); currentCallable: fn() => $this->assertMenuLinks([
'/' => 'Home',
], 'Custom link targeting <current> is not visible in menu with permission to link to any page.'),
deprecatedCallable: fn() => $this->assertMenuLinks([
'/' => 'Home',
'/admin/structure/menu/item/1/edit' => 'link_current',
], 'Custom link targeting <current> is visible in menu with permission to link to any page.'),
);
$this->drupalGet(''); $this->drupalGet('');
$this->assertSession()->statusCodeEquals(200); $this->assertSession()->statusCodeEquals(200);
$this->assertMenuLinks([ DeprecationHelper::backwardsCompatibleCall(
'/' => 'Home', currentVersion: \Drupal::VERSION,
'/user/' . $this->users['admin_menu_any']->id() => 'link_current', deprecatedVersion: '10.3',
], 'Custom link targeting <current> is visible in menu with permission to link to any page.'); currentCallable: fn() => $this->assertMenuLinks([
'/' => 'Home',
], 'Custom link targeting <current> is not visible in menu with permission to link to any page.'),
deprecatedCallable: fn() => $this->assertMenuLinks([
'/' => 'Home',
'/user/' . $this->users['admin_menu_any']->id() => 'link_current',
], 'Custom link targeting <current> is visible in menu with permission to link to any page.'),
);
$this->drupalLogin($this->users['view_links']); $this->drupalLogin($this->users['view_links']);
$this->assertMenuLinks([ $this->assertMenuLinks([
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment