diff --git a/config_entity_example/tests/src/Functional/ConfigEntityExampleTest.php b/config_entity_example/tests/src/Functional/ConfigEntityExampleTest.php index c3aedb7..4e48f14 100644 --- a/config_entity_example/tests/src/Functional/ConfigEntityExampleTest.php +++ b/config_entity_example/tests/src/Functional/ConfigEntityExampleTest.php @@ -36,14 +36,14 @@ class ConfigEntityExampleTest extends BrowserTestBase { */ public static function getInfo() { return array( - 'name' => 'Config Entity Example functional test', + 'name' => 'Config Entity Example Functional test', 'description' => 'Test the Config Entity Example module.', 'group' => 'Examples', ); } /** - * Various functional test of the Config Entity Example module. + * Various Functional test of the Config Entity Example module. * * 1) Verify that the Marvin entity was created when the module was installed. * diff --git a/menu_example/menu_example.links.menu.yml b/menu_example/menu_example.links.menu.yml index a214cfc..dbebb0c 100644 --- a/menu_example/menu_example.links.menu.yml +++ b/menu_example/menu_example.links.menu.yml @@ -34,10 +34,10 @@ examples.menu_example.custom_access_page: parent: examples.menu_example.custom_access route_name: examples.menu_example.custom_access_page -examples.menu_example.path_only: +examples.menu_example.route_only: title: Route only example parent: examples.menu_example - route_name: examples.menu_example.path_only + route_name: examples.menu_example.route_only weight: 20 examples.menu_example.tabs: @@ -67,10 +67,3 @@ examples.menu_example.placeholder_argument: parent: examples.menu_example route_name: examples.menu_example.placeholder_argument weight: 60 - -example.menu_example.default_arg.arg_optional: - title: Processed placeholder Arguments - description: '' - parent: examples.menu_example - route_name: examples.menu_example.default_arg.arg_optional - weight: 60 diff --git a/menu_example/menu_example.routing.yml b/menu_example/menu_example.routing.yml index f5afe89..a152b6f 100644 --- a/menu_example/menu_example.routing.yml +++ b/menu_example/menu_example.routing.yml @@ -18,7 +18,7 @@ examples.menu_example.permissioned: path: '/examples/menu-example/permissioned' defaults: _controller: '\Drupal\menu_example\Controller\MenuExampleController::permissioned' - _title: 'Permissioned Example' + _title: 'Permissioned Route Example' requirements: _access: 'TRUE' @@ -26,7 +26,7 @@ examples.menu_example.permissioned_controlled: path: '/examples/menu-example/permissioned/controlled' defaults: _controller: '\Drupal\menu_example\Controller\MenuExampleController::permissionedControlled' - _title: 'Permissioned Menu Item' + _title: 'Permissioned Route' requirements: _permission: 'access protected menu example' @@ -44,20 +44,20 @@ examples.menu_example.custom_access_page: _controller: '\Drupal\menu_example\Controller\MenuExampleController::customAccessPage' _title: 'Custom Access Menu Item' requirements: - _custom_access: '\Drupal\menu_example\Controller\MenuExampleController::getRole' + _menu_example_role: 'TRUE' -examples.menu_example.path_only: - path: '/examples/menu-example/path-only' +examples.menu_example.route_only: + path: '/examples/menu-example/route-only' defaults: - _controller: '\Drupal\menu_example\Controller\MenuExampleController::pathOnly' - _title: 'Menu_Callback example' + _controller: '\Drupal\menu_example\Controller\MenuExampleController::routeOnly' + _title: 'Route only example' requirements: _access: 'TRUE' examples.menu_example.callback: - path: '/examples/menu-example/path-only/callback' + path: '/examples/menu-example/route-only/callback' defaults: - _controller: '\Drupal\menu_example\Controller\MenuExampleController::pathOnlycallback' + _controller: '\Drupal\menu_example\Controller\MenuExampleController::routeOnlyCallback' _title: 'Callback only' requirements: _access: 'TRUE' @@ -97,7 +97,7 @@ examples.menu_example.tabnameFour: path: 'examples/menu-example/tabs/forth' defaults: _title: 'Tabs' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::tabNameForth' + _controller: '\Drupal\menu_example\Controller\MenuExampleController::tabNameFourth' requirements: _access: 'TRUE' @@ -118,26 +118,12 @@ examples.menu_example.defaultThird: _access: 'TRUE' examples.menu_example.use_url_arguments: - path: 'examples/menu-example/use-url-arguments' - defaults: - _title: 'Extra Agruments' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::urlArgument' - requirements: - _access: 'TRUE' - -examples.menu_example.use_url_arguments1: - path: 'examples/menu-example/use-url-arguments/{arg1}' - defaults: - _title: 'Extra Agruments' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::urlArgument1' - requirements: - _access: 'TRUE' - -examples.menu_example.use_url_arguments2: path: 'examples/menu-example/use-url-arguments/{arg1}/{arg2}' defaults: - _title: 'Extra Agruments' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::urlArgument2' + _title: 'URL Arguments' + _controller: '\Drupal\menu_example\Controller\MenuExampleController::urlArgument' + arg1: '' + arg2: '' requirements: _access: 'TRUE' @@ -153,31 +139,23 @@ examples.menu_example.placeholder_argument: path: 'examples/menu-example/placeholder-argument' defaults: _title: 'Placeholder Arguments' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::argument' + _controller: '\Drupal\menu_example\Controller\MenuExampleController::placeholderArgs' requirements: _access: 'TRUE' examples.menu_example.placeholder_argument.display: - path: 'examples/menu-example/placeholder-argument/{node}/display' + path: 'examples/menu-example/placeholder-argument/{arg}/display' defaults: _title: 'Placeholder Arguments' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::display' + _controller: '\Drupal\menu_example\Controller\MenuExampleController::placeholderArgsDisplay' + arg: '' requirements: _access: 'TRUE' -examples.menu_example.default_arg.arg_optional: - path: 'examples/menu-example/default_arg/{no}' - defaults: - _title: 'Processed Placeholder Arguments' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::argOptional' - no: 99 - requirements: - _access: 'TRUE' - -example.menu_example.menu_original_path: - path: 'examples/menu-example/menu_original_path' +example.menu_example.path_override: + path: 'examples/menu-example/menu-original-path' defaults: - _title: 'Menu path that will be altered by hook_menu_alter()' - _controller: '\Drupal\menu_example\Controller\MenuExampleController::orginalPath' + _title: 'Menu path that will be altered' + _controller: '\Drupal\menu_example\Controller\MenuExampleController::pathOverride' requirements: _access: 'TRUE' diff --git a/menu_example/menu_examples.services.yml b/menu_example/menu_examples.services.yml index e69de29..b62e673 100644 --- a/menu_example/menu_examples.services.yml +++ b/menu_example/menu_examples.services.yml @@ -0,0 +1,10 @@ +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/Access/RoleAccessCheck.php b/menu_example/src/Access/RoleAccessCheck.php index e69de29..aae54b8 100644 --- a/menu_example/src/Access/RoleAccessCheck.php +++ b/menu_example/src/Access/RoleAccessCheck.php @@ -0,0 +1,27 @@ +isAuthenticated()); + } + +} diff --git a/menu_example/src/Controller/MenuExampleController.php b/menu_example/src/Controller/MenuExampleController.php index 125c819..0e9b3d3 100644 --- a/menu_example/src/Controller/MenuExampleController.php +++ b/menu_example/src/Controller/MenuExampleController.php @@ -5,34 +5,37 @@ namespace Drupal\menu_example\Controller; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Link; use Drupal\Core\Url; -use Drupal\Core\Access\AccessResult; -use Drupal\Core\Session\AccountInterface; use Drupal\examples\Utility\DescriptionTemplateTrait; /** * Controller routines for menu example routes. */ class MenuExampleController extends ControllerBase { + use DescriptionTemplateTrait; + + /** + * {@inheritdoc} + */ + protected function getModuleName() { + return 'menu_example'; + } /** * Page callback for the simplest introduction menu entry. - * The router _controller callback, maps the path - * 'examples/menu-example' to this method. + * + * The controller callback defined menu_examples.routing.yml file, + * maps the path 'examples/menu-example' to this method. + * + * @throws \InvalidArgumentException */ public function basicInstructions() { - $url = Url::fromUri('internal:/examples/menu-example/path-only'); - $link = Link::fromTextAndUrl($this->t('visit a similar page with no menu link'), $url)->toString(); return [ - '#markup' => $this->t('This page is displayed by the simplest (and base) - menu example. Note that the title of the page is the same as the link - title. You can also @link. There are a number of - examples here, from the most basic (like this one) to extravagant - mappings of loaded placeholder arguments. Enjoy!', ['@link' => $link]), + $this->description(), ]; } /** - *Show a menu link in a menu other than the default "Navigation" menu. + * Show a menu link in a menu other than the default "Navigation" menu. */ public function alternateMenu() { return [ @@ -42,7 +45,9 @@ class MenuExampleController extends ControllerBase { } /** - *A menu entry with simple permissions using 'access protected menu example'. + * A menu entry with simple permissions using 'access protected menu example'. + * + * @throws \InvalidArgumentException */ public function permissioned() { $url = Url::fromUri('internal:/examples/menu-example/permissioned/controlled'); @@ -53,17 +58,20 @@ class MenuExampleController extends ControllerBase { } /** - *Now the actual permissioned menu item. - *For the permissioned menu we have defined it under menu_example/menu_example.routing.yml + * Only accessible when the user will be granted with required permission. + * + * The permission is defined in file menu_examples.permissions.yml. */ public function permissionedControlled() { return [ - '#markup' => $this->t('This menu entry will not show and the page will not be accessible without the "access protected menu example" permission.'), + '#markup' => $this->t('This menu entry will not show and the page will not be accessible without the "access protected menu example" permission to current user.'), ]; } /** + * Demonstrates the use of custom access check in routes. * + * @throws \InvalidArgumentException */ public function customAccess() { $url = Url::fromUri('internal:/examples/menu-example/custom-access-page'); @@ -74,18 +82,41 @@ class MenuExampleController extends ControllerBase { } /** - * + * Content will be displayed only if access check is satisfied. */ - public function pathonly() { - $url = Url::fromUri('internal:/examples/menu-example/path-only/callback'); - $link = Link::fromTextAndUrl($this->t('examples/menu-example/path-only/callback'), $url)->toString(); + public function customAccessPage() { return [ - '#markup' => $this->t('A menu entry with no menu link (MENU_CALLBACK) is at @link', ['@link' => $link]), + '#markup' => $this->t('This menu entry will not be visible and access will result + in a 403 error unless the user has the "authenticated" role. This is + accomplished with a custom access check plugin.'), ]; } /** + * Only callback without a menu link. * + * @throws \InvalidArgumentException + */ + public function routeOnly() { + $url = Url::fromUri('internal:/examples/menu-example/route-only/callback'); + $link = Link::fromTextAndUrl($this->t('examples/menu-example/route-only/callback'), $url)->toString(); + return [ + '#markup' => $this->t('A menu entry with no menu link is at @link', ['@link' => $link]), + ]; + } + + /** + * Such callbacks can be user for creating web services in Drupal 8. + */ + public function routeOnlyCallback() { + return [ + '#markup' => $this->t('The route entry has no corresponding menu links entry, so it provides a route without a menu link, but it is the same in every other way to the simplest example.'), + ]; + } + + + /** + * A simple menu entry to display tabs. */ public function tabs() { return [ @@ -94,7 +125,7 @@ class MenuExampleController extends ControllerBase { } /** - * + * Callback for returning second tab. */ public function tabNameSecond() { return [ @@ -103,7 +134,7 @@ class MenuExampleController extends ControllerBase { } /** - * + * Callback for returning third tab. */ public function tabNameThird() { return [ @@ -112,16 +143,16 @@ class MenuExampleController extends ControllerBase { } /** - * + * Callback for returning fourth tab. */ - public function tabNameForth() { + public function tabNameFourth() { return [ - '#markup' => $this->t('This is the tab "Forth" in the "basic tabs" example'), + '#markup' => $this->t('This is the tab "Fourth" in the "basic tabs" example'), ]; } /** - * + * Callback for second secondary tab. */ public function defaultSecond() { return [ @@ -130,7 +161,7 @@ class MenuExampleController extends ControllerBase { } /** - * + * Callback for third secondary tab. */ public function defaultThird() { return [ @@ -139,61 +170,59 @@ class MenuExampleController extends ControllerBase { } /** + * Demonstrates use of url arguments in for menu item. * + * @throws \InvalidArgumentException */ - public function urlArgument() { - $url = Url::fromUri('internal:/examples/menu-example/use-url-arguments/firstarg/secondarg'); - $link = Link::fromTextAndUrl($this->t('examples/menu-example/use-url-arguments/firstarg/secondarg'), $url)->toString(); + public function urlArgument($arg1, $arg2) { + // Perpare link for single arguments. + $url = Url::fromUri('internal:/examples/menu-example/use-url-arguments/one'); + $link = Link::fromTextAndUrl($this->t('examples/menu-example/use-url-arguments/one'), $url)->toString(); + + // Prepare link for multiple arguments. $urltwo = Url::fromUri('internal:/examples/menu-example/use-url-arguments/one/two'); $linktwo = Link::fromTextAndUrl($this->t('examples/menu-example/use-url-arguments/one/two'), $urltwo)->toString(); - return [ - '#markup' => $this->t('This page demonstrates using arguments in the path (portions of the path after "menu_example/use-url-arguments". For example, access it with @link or @linktwo', ['@link' => $link, '@linktwo' => $linktwo]), - ]; - } + $markup = $this->t('This page demonstrates using arguments in the url. For example, access it with @link for single argument or @linktwo for two arguments in URL', ['@link' => $link, '@linktwo' => $linktwo]); - /** - * - */ - public function urlArgument1($arg1 = '') { - return [ - '#markup' => $this->t('Argument1 = @arg1', ['@arg1' => $arg1]), - ]; - } + if (!empty($arg1)) { + $markup .= '
This page is displayed by the simplest (and base) menu example. Note that the title of the page is the same as the link title. You can also visit a similar page with no menu link. +There are a number of examples here, from the most basic (like this one) to extravagant mappings of loaded placeholder arguments. Enjoy!
+ +{% endtrans %} diff --git a/menu_example/tests/src/Functional/MenuExampleTest.php b/menu_example/tests/src/Functional/MenuExampleTest.php index b9686a1..0d46995 100644 --- a/menu_example/tests/src/Functional/MenuExampleTest.php +++ b/menu_example/tests/src/Functional/MenuExampleTest.php @@ -1,31 +1,34 @@ drupalGet(''); @@ -35,37 +38,34 @@ class MenuExampleTest extends BrowserTestBase { $this->clickLink(t('Custom Access Example')); $this->assertText(t('Custom Access Example')); - $this->drupalGet('examples/menu_example/permissioned'); + $this->drupalGet('examples/menu-example/permissioned'); $this->assertText(t('Permissioned Example')); - $this->clickLink('examples/menu_example/permissioned/controlled'); + $this->clickLink('examples/menu-example/permissioned/controlled'); $this->assertResponse(403); - $this->drupalGet('examples/menu_example'); + $this->drupalGet('examples/menu-example'); - $this->clickLink(t('MENU_CALLBACK example')); + $this->clickLink(t('Route only example')); $this->clickLink(t('Tabs')); $this->assertText(t('This is the "tabs" menu entry')); - $this->drupalGet('examples/menu_example/tabs/second'); + $this->drupalGet('examples/menu-example/tabs/second'); $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('Extra Arguments')); + $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->drupalGet('examples/menu-example/use-url-arguments/one/two'); + $this->assertText(t('Argument 1 = one Argument 2 = two')); $this->clickLink(t('Placeholder Arguments')); - $this->clickLink(t('examples/menu_example/placeholder-argument/3343/display')); - $this->assertRaw('