reverted: --- b/examples.module +++ a/examples.module @@ -50,7 +50,6 @@ 'file_example' => 'file_example.fileapi', 'hooks_example' => 'hooks_example.description', 'js_example' => 'js_example.info', - 'menu_example' => 'examples.menu_example', 'node_type_example' => 'config_node_type_example.description', 'page_example' => 'page_example_description', 'pager_example' => 'pager_example.page', diff -u b/menu_example/menu_example.routing.yml b/menu_example/menu_example.routing.yml --- b/menu_example/menu_example.routing.yml +++ b/menu_example/menu_example.routing.yml @@ -24,7 +24,7 @@ _controller: '\Drupal\menu_example\Controller\MenuExampleController::alternateMenu' _title: 'Menu Example: Menu in alternate menu' requirements: - _access: 'TRUE' + _permission: 'access content' # The menu entry demonstrates the use of permissions to restrict access # to a particular path until and unless the requirement is met. @@ -34,7 +34,7 @@ _controller: '\Drupal\menu_example\Controller\MenuExampleController::permissioned' _title: 'Permissioned Route Example' requirements: - _access: 'TRUE' + _permission: 'access content' # Only the user with the required permission will be able to access this route. examples.menu_example.permissioned_controlled: @@ -72,7 +72,7 @@ _controller: '\Drupal\menu_example\Controller\MenuExampleController::routeOnly' _title: 'Route only example' requirements: - _access: 'TRUE' + _permission: 'access content' # These type of routes can be used to create webservices. examples.menu_example.callback: @@ -81,7 +81,7 @@ _controller: '\Drupal\menu_example\Controller\MenuExampleController::routeOnlyCallback' _title: 'Callback only' requirements: - _access: 'TRUE' + _permission: 'access content' # Drupal 8's routes may include placeholder elements which designate places # where the URL contains dynamic values. In the controller method, this value @@ -97,7 +97,7 @@ arg1: '' arg2: '' requirements: - _access: 'TRUE' + _permission: 'access content' # Title of this menu entry will be altered by controller method defined by # key '_title_callback'. @@ -107,7 +107,7 @@ _title_callback: 'Drupal\menu_example\Controller\MenuExampleController::backTitle' _controller: '\Drupal\menu_example\Controller\MenuExampleController::titleCallback' requirements: - _access: 'TRUE' + _permission: 'access content' # A menu item entry to demonstrate use of parameters in routes. examples.menu_example.placeholder_argument: @@ -116,7 +116,7 @@ _title: 'Placeholder Arguments' _controller: '\Drupal\menu_example\Controller\MenuExampleController::placeholderArgs' requirements: - _access: 'TRUE' + _permission: 'access content' # Parameters in routes can be used as placeholders arguments too. examples.menu_example.placeholder_argument.display: @@ -126,7 +126,7 @@ _controller: '\Drupal\menu_example\Controller\MenuExampleController::placeholderArgsDisplay' requirements: arg: \d+ - _access: 'TRUE' + _permission: 'access content' # Any route defined here can be altered. We can do so by defining a RouteSubscriber! example.menu_example.path_override: @@ -135,7 +135,7 @@ _title: 'Menu path that will be altered' _controller: '\Drupal\menu_example\Controller\MenuExampleController::pathOverride' requirements: - _access: 'TRUE' + _permission: 'access content' # Dynamic routes are implemented in a method. This method is added as a # 'route_callbacks' diff -u b/menu_example/src/Controller/MenuExampleController.php b/menu_example/src/Controller/MenuExampleController.php --- b/menu_example/src/Controller/MenuExampleController.php +++ b/menu_example/src/Controller/MenuExampleController.php @@ -4,8 +4,8 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Link; -use Drupal\Core\Url; use Drupal\examples\Utility\DescriptionTemplateTrait; + /** * Controller routines for menu example routes. * @@ -62,8 +62,8 @@ * @throws \InvalidArgumentException */ public function permissioned() { - $url = Url::fromUri('internal:/examples/menu-example/permissioned/controlled'); - $link = Link::fromTextAndUrl($this->t('examples/menu-example/permissioned/controlled'), $url)->toString(); + $link = Link::createFromRoute($this->t('examples/menu-example/permissioned/controlled'), + 'examples.menu_example.permissioned_controlled')->toString(); return [ '#markup' => $this->t('A menu item that requires the "access protected menu example" permission is at @link', ['@link' => $link]), ]; @@ -84,12 +84,12 @@ * Demonstrates the use of custom access check in routes. * * @throws \InvalidArgumentException - + * * @see \Drupal\menu_example\Controller\MenuExampleController::customAccessPage() */ 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(); + $link = Link::createFromRoute($this->t('examples/menu-example/custom-access/page'), + 'examples.menu_example.custom_access_page')->toString(); return [ '#markup' => $this->t('A menu item that requires the user to posess a role of "authenticated" is at @link', ['@link' => $link]), ]; @@ -114,8 +114,8 @@ * @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(); + $link = Link::createFromRoute($this->t('examples/menu-example/route-only/callback'), + 'examples.menu_example.callback')->toString(); return [ '#markup' => $this->t('A menu entry with no menu link is at @link', ['@link' => $link]), ]; @@ -142,9 +142,9 @@ */ public function tabsPage($path, $title) { $secondary = substr_count($path, '/') > 2 ? 'secondary ' : ''; - return array( + return [ '#markup' => $this->t('This is the @secondary tab "@tabname" in the "basic tabs" example.', ['@secondary' => $secondary, '@tabname' => $title]), - ); + ]; } /** @@ -162,20 +162,20 @@ */ 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(); + $link = Link::createFromRoute($this->t('examples/menu-example/use-url-arguments/one'), + 'examples.menu_example.use_url_arguments', ['arg1' => 'one'])->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(); + $linktwo = Link::createFromRoute($this->t('examples/menu-example/use-url-arguments/one/two'), + 'examples.menu_example.use_url_arguments', ['arg1' => 'one', 'arg2' => 'two'])->toString(); $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]); if (!empty($arg1)) { - $markup .= '
' . $this->t('Argument 1 = @arg', array('@arg' => $arg1)) . '
'; + $markup .= '
' . $this->t('Argument 1 = @arg', ['@arg' => $arg1]) . '
'; } if (!empty($arg2)) { - $markup .= '
' . $this->t('Argument 2 = @arg', array('@arg' => $arg2)) . '
'; + $markup .= '
' . $this->t('Argument 2 = @arg', ['@arg' => $arg2]) . '
'; } return [ @@ -201,7 +201,10 @@ * @see \Drupal\menu_example\Controller\MenuExampleController::titleCallback() */ public function backTitle() { - return \Drupal::currentUser()->getDisplayName(); + $name = \Drupal::currentUser()->getDisplayName(); + return [ + '#markup' => $this->t('The new title is your username: @name', ['@name' => $name]), + ]; } /** @@ -213,8 +216,8 @@ * @see https://www.drupal.org/docs/8/api/routing-system/using-parameters-in-routes */ public function placeholderArgs() { - $url = Url::fromUri('internal:/examples/menu-example/placeholder-argument/3343/display'); - $link = Link::fromTextAndUrl($this->t('examples/menu-example/placeholder-argument/3343/display'), $url)->toString(); + $link = Link::createFromRoute($this->t('examples/menu-example/placeholder-argument/3343/display'), + 'examples.menu_example.placeholder_argument.display', ['arg' => 3343])->toString(); return [ '#markup' => $this->t('Demonstrate placeholders by visiting @link', ['@link' => $link]), ]; diff -u b/menu_example/templates/description.html.twig b/menu_example/templates/description.html.twig --- b/menu_example/templates/description.html.twig +++ b/menu_example/templates/description.html.twig @@ -1,6 +1,6 @@ {# -Description text for the Block Example. +Description text for the Menu Example. #} diff -u b/menu_example/tests/src/Functional/MenuExampleTest.php b/menu_example/tests/src/Functional/MenuExampleTest.php --- b/menu_example/tests/src/Functional/MenuExampleTest.php +++ b/menu_example/tests/src/Functional/MenuExampleTest.php @@ -4,7 +4,7 @@ use Drupal\Tests\BrowserTestBase; use Drupal\user\Entity\Role; -use Drupal\user\RoleInterface; +use Drupal\Core\Url; /** * Test the functionality for the menu Example. @@ -21,7 +21,7 @@ * * @var array */ - public static $modules = array('menu_example'); + public static $modules = ['menu_example']; /** * The installation profile to use with this test. * @@ -46,30 +46,30 @@ $this->assertSession()->pageTextContains(t("@name", ['@name' => $web_user->getUsername()])); // Now start testing other menu entries. - $this->drupalGet('examples/menu-example'); + $this->drupalGet(Url::fromRoute('examples.menu_example')); $this->clickLink(t('Custom Access Example')); $this->assertSession()->pageTextContains(t('Custom Access Example')); - $this->drupalGet('examples/menu-example/custom-access/page'); + $this->drupalGet(Url::fromRoute('examples.menu_example')); $this->assertSession()->statusCodeEquals(200); // Logout user to check denial of access. $this->drupalLogout(); - $this->drupalGet('examples/menu-example/custom-access/page'); + $this->drupalGet(Url::fromRoute('examples.menu_example')); $this->assertSession()->statusCodeEquals(403); // Check 'Permission Example' menu entry. $this->drupalLogin($web_user); - $this->drupalGet('examples/menu-example/permissioned'); + $this->drupalGet(Url::fromRoute('examples.menu_example.permissioned')); $this->assertSession()->pageTextContains(t('Permissioned Example')); - $this->clickLink('examples/menu-example/permissioned/controlled'); + $this->clickLink(Url::fromRoute('examples.menu_example.permissioned_controlled')); $this->assertSession()->statusCodeEquals(403); // Check access with appropriate permissions. $this->grantPermissions(Role::load(Role::AUTHENTICATED_ID), ['access protected menu example']); - $this->drupalGet('examples/menu-example/permissioned/controlled'); + $this->drupalGet(Url::fromRoute('examples.menu_example.permissioned_controlled')); $this->assertSession()->statusCodeEquals(200); // Check Route only example entry. $this->clickLink(t('Route only example')); - $this->clickLink('examples/menu-example/route-only/callback'); + $this->clickLink(Url::fromRoute('examples.menu_example.callback')); $this->assertSession()->pageTextContains('The route entry has no corresponding menu links entry'); // Check links on Tabs landing page. @@ -90,7 +90,7 @@ // Now verify 'URL arguments' menu entry to be working with two arguments. $this->clickLink(t('URL Arguments')); - $this->drupalGet('examples/menu-example/use-url-arguments/one/two'); + $this->drupalGet(Url::fromRoute('examples.menu_example.use_url_arguments', ['arg1' => 'one', 'arg2' => 'two'])); $this->assertSession()->pageTextContains(t('Argument 1 = one')); $this->assertSession()->pageTextContains(t('Argument 2 = two'));