diff --git a/menu_example/menu_example.links.menu.yml b/menu_example/menu_example.links.menu.yml index dbebb0c..d5865c7 100644 --- a/menu_example/menu_example.links.menu.yml +++ b/menu_example/menu_example.links.menu.yml @@ -48,7 +48,7 @@ examples.menu_example.tabs: weight: 30 examples.menu_example.use_url_arguments: - title: Extra Arguments + title: URL Arguments description: 'The page callback can use the arguments provided after the path used as key' parent: examples.menu_example route_name: examples.menu_example.use_url_arguments diff --git a/menu_example/menu_example.routing.yml b/menu_example/menu_example.routing.yml index a152b6f..888bc06 100644 --- a/menu_example/menu_example.routing.yml +++ b/menu_example/menu_example.routing.yml @@ -148,9 +148,9 @@ examples.menu_example.placeholder_argument.display: defaults: _title: 'Placeholder Arguments' _controller: '\Drupal\menu_example\Controller\MenuExampleController::placeholderArgsDisplay' - arg: '' requirements: - _access: 'TRUE' + arg: \d+ + _access: 'TRUE' example.menu_example.path_override: path: 'examples/menu-example/menu-original-path' diff --git a/menu_example/menu_example.services.yml b/menu_example/menu_example.services.yml index e69de29..f2e934e 100644 --- a/menu_example/menu_example.services.yml +++ b/menu_example/menu_example.services.yml @@ -0,0 +1,11 @@ +services: + menu_example.access_check.role: + class: Drupal\menu_example\Access\RoleAccessCheck + arguments: ['@current_user'] + tags: + - { name: access_check, applies_to: _menu_example_role } + + menu_example.route_subscriber: + class: Drupal\menu_example\Routing\RouteSubscriber + tags: + - { name: event_subscriber } diff --git a/menu_example/menu_examples.services.yml b/menu_example/menu_examples.services.yml deleted file mode 100644 index b62e673..0000000 --- a/menu_example/menu_examples.services.yml +++ /dev/null @@ -1,10 +0,0 @@ -services: - menu_example.access_check.role: - class: Drupal\menu_example\Access\RoleAccessCheck - tags: - - { name: access_check, applies_to: _menu_example_role } - - menu_example.route_subscriber: - class: Drupal\menu_example\Routing\RouteSubscriber - tags: - - { name: event_subscriber } diff --git a/menu_example/src/Controller/MenuExampleController.php b/menu_example/src/Controller/MenuExampleController.php index 0e9b3d3..ef95755 100644 --- a/menu_example/src/Controller/MenuExampleController.php +++ b/menu_example/src/Controller/MenuExampleController.php @@ -74,8 +74,8 @@ class MenuExampleController extends ControllerBase { * @throws \InvalidArgumentException */ public function customAccess() { - $url = Url::fromUri('internal:/examples/menu-example/custom-access-page'); - $link = Link::fromTextAndUrl($this->t('examples/menu-example/custom-access-page'), $url)->toString(); + $url = Url::fromUri('internal:/examples/menu-example/custom-access/page'); + $link = Link::fromTextAndUrl($this->t('examples/menu-example/custom-access/page'), $url)->toString(); return [ '#markup' => $this->t('A menu item that requires the user to posess a role of "authenticated" is at @link', ['@link' => $link]), ]; @@ -211,10 +211,7 @@ class MenuExampleController extends ControllerBase { * Generates title dynamically. */ public function backTitle() { - $user = \Drupal::currentUser(); - return [ - $title = $user->getDisplayName(), - ]; + return \Drupal::currentUser()->getDisplayName(); } /** @@ -233,7 +230,7 @@ class MenuExampleController extends ControllerBase { /** * Displays placeholder argument supplied in URL. * - * @param string $arg + * @param int $arg * URL argument. * * @return array diff --git a/menu_example/src/Routing/RouteSubscriber.php b/menu_example/src/Routing/RouteSubscriber.php index a0b39ff..a6e198c 100644 --- a/menu_example/src/Routing/RouteSubscriber.php +++ b/menu_example/src/Routing/RouteSubscriber.php @@ -14,12 +14,11 @@ class RouteSubscriber extends RouteSubscriberBase { */ public function alterRoutes(RouteCollection $collection) { // Get the path from RouteCollection. - $route = $collection->get('example.menu_example.menu_original_path'); + $route = $collection->get('example.menu_example.path_override'); // Set the new path. $route->setPath('/examples/menu-example/menu-altered-path'); // Change title to indicate changes. $route->setDefault('_title', 'Menu item altered by RouteSubscriber::alterRoutes'); - } } diff --git a/menu_example/tests/src/Functional/MenuExampleTest.php b/menu_example/tests/src/Functional/MenuExampleTest.php index 0d46995..ba4bef7 100644 --- a/menu_example/tests/src/Functional/MenuExampleTest.php +++ b/menu_example/tests/src/Functional/MenuExampleTest.php @@ -1,6 +1,6 @@ drupalGet(''); $this->clickLink(t('Menu Example')); - $this->assertText(t('This is the base page of the Menu Example')); - - $this->clickLink(t('Custom Access Example')); - $this->assertText(t('Custom Access Example')); + $this->assertText(t('This page is displayed by the simplest (and base) menu example')); $this->drupalGet('examples/menu-example/permissioned'); $this->assertText(t('Permissioned Example')); @@ -44,6 +41,24 @@ class MenuExampleTest extends BrowserTestBase { $this->clickLink('examples/menu-example/permissioned/controlled'); $this->assertResponse(403); + // Create a user with permissions to access protected menu entry. + $web_user = $this->drupalCreateUser(); + + // Use custom overridden drupalLogin function to verify the user is logged + // in. + $this->drupalLogin($web_user); + + // Check that our title callback changing /user dynamically is working. + $this->assertText(t("@name", ['@name' => $web_user->getUsername()])); + + // Now start testing other menu entries. + $this->drupalGet('examples/menu-example'); + + $this->clickLink(t('Custom Access Example')); + $this->assertText(t('Custom Access Example')); + $this->drupalGet('examples/menu-example/custom-access/page'); + $this->assertResponse(200); + $this->drupalGet('examples/menu-example'); $this->clickLink(t('Route only example')); @@ -52,47 +67,24 @@ class MenuExampleTest extends BrowserTestBase { $this->assertText(t('This is the "tabs" menu entry')); $this->drupalGet('examples/menu-example/tabs/second'); - $this->assertText(t('This is the tab "second" in the "basic tabs" example')); + $this->assertText(t('This is the tab "Second" in the "basic tabs" example')); - $this->clickLink(t('third')); - $this->assertText(t('This is the tab "third" in the "basic tabs" example')); + $this->clickLink(t('three')); + $this->assertText(t('This is the tab "Thrid" in the "basic tabs" example')); $this->clickLink(t('URL Arguments')); $this->drupalGet('examples/menu-example/use-url-arguments/one/two'); - $this->assertText(t('Argument 1 = one Argument 2 = two')); + $this->assertText(t('Argument 1 = one')); + $this->assertText(t('Argument 2 = two')); $this->clickLink(t('Placeholder Arguments')); $this->clickLink(t('examples/menu-example/placeholder-argument/3343/display')); $this->assertText('3343'); - // Create a user with permissions to access protected menu entry. - $web_user = $this->drupalCreateUser(array('access protected menu example')); - - // Use custom overridden drupalLogin function to verify the user is logged - // in. - $this->drupalLogin($web_user); - - // Check that our title callback changing /user dynamically is working. - // Using ' because of the format_username function. - $this->assertText(t("@name's account", array('@name' => $web_user->getUsername())), format_string('Title successfully changed to account name: %name.', array('%name' => $web_user->getUsername()))); - - // Now start testing other menu entries. - $this->drupalGet('examples/menu-example'); - - $this->clickLink(t('Custom Access Example')); - $this->assertText(t('Custom Access Example')); - - $this->drupalGet('examples/menu-example/custom-access/page'); - $this->assertResponse(200); - - $this->drupalGet('examples/menu-example/permissioned'); - $this->assertText('Permissioned Example'); - $this->clickLink('examples/menu-example/permissioned/controlled'); - $this->assertText('This menu entry will not show'); - - $this->drupalGet('examples/menu_altered_path'); + $this->drupalGet('examples/menu-example/menu-altered-path'); + $this->assertText('Menu item altered by RouteSubscriber::alterRoutes'); }