diff --git a/menu_example/menu_example.info.yml b/menu_example/menu_example.info.yml new file mode 100644 index 0000000..4778cc5 --- /dev/null +++ b/menu_example/menu_example.info.yml @@ -0,0 +1,7 @@ +name: Menu Example +type: module +description: 'An example module showing the main steps to define and handling menu links in Drupal 8 ' +package: 'Example module' +core: 8.x +dependencies: + - examples diff --git a/menu_example/menu_example.links.task.yml b/menu_example/menu_example.links.task.yml new file mode 100644 index 0000000..06ba518 --- /dev/null +++ b/menu_example/menu_example.links.task.yml @@ -0,0 +1,22 @@ +examples.menu_example.tabdefault: + route_name: examples.menu_example.tabs + title: 'Default Primary Tab' + parent_id: examples.menu_example.tabs + +examples.menu_example.tabOne: + route_name: examples.menu_example.tabname + title: 'two' + parent_id: examples.menu_example.tabs + weight: 2 + +examples.menu_example.tabTwo: + route_name: examples.menu_example.tabname + title: 'three' + parent_id: examples.menu_example.tabs + weight: 3 + +examples.menu_example.tabThree: + route_name: examples.menu_example.tabname + title: 'four' + parent_id: examples.menu_example.tabs + weight: 4 diff --git a/menu_example/menu_example.menu.links.yml b/menu_example/menu_example.menu.links.yml new file mode 100644 index 0000000..e69de29 diff --git a/menu_example/menu_example.module b/menu_example/menu_example.module new file mode 100644 index 0000000..e2b5e72 --- /dev/null +++ b/menu_example/menu_example.module @@ -0,0 +1,36 @@ + array( + 'title' => t('Access the protected menu example'), + ), + ); +} + +/* + * Implements hook_menu_link_presave(). + */ +function menu_example_menu_link_presave(MenuLink $menu_link) { + // Hide the "User account" link for anonymous users. + if ($menu_link->machine_name == 'examples.menu_example.title_callbacks') { + $menu_link->options['alter'] = TRUE; + } +} + +/** + * Implements hook_translated_menu_link_alter(). + */ +function menu_example_translated_menu_link_alter(MenuLink &$menu_link) { + if ($menu_link->machine_name == 'examples.menu_example.title_callbacks') { + global $user; + $menu_link->title = t('Dynamic title: username=@username', array('@username' => $user->name)); + } +} diff --git a/menu_example/menu_example.routing.yml b/menu_example/menu_example.routing.yml new file mode 100644 index 0000000..6ee69d2 --- /dev/null +++ b/menu_example/menu_example.routing.yml @@ -0,0 +1,167 @@ +examples.menu_example: + path: '/examples/menu_example' + default: + _content: '\Drupal\menu_example\Controller\MenuExampleController::basicInstructions' + _title: 'Menu Example' + requirements: + _permission: 'access content' + +examples.menu_example.alternate_menu: + path: '/examples/menu_example_alternate_menu' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::alternateMenu' + _title: 'Menu Example: Menu in alternate menu' + requirements: + _access: 'TRUE' + +examples.menu_example.permissioned: + path: '/examples/menu_example/permissioned' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::permissioned' + _title: 'Permissioned Example' + requirements: + _access: 'TRUE' + +examples.menu_example.permissioned_controlled: + path: '/examples/menu_example/permissioned/controlled' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::permissionedControlled' + _title: 'Permissioned Menu Item' + requirements: + _permission: 'access protected menu example' + +examples.menu_example.custom_access: + path: '/examples/menu_example/custom_access' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::customAccess' + _title: 'Custom Access Example' + requirements: + _permission: 'access content' + +examples.menu_example.custom_access_page: + path: '/examples/menu_example/custom_access/page' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::customAccessPage' + _title: 'Custom Access Menu Item' + requirements: + _permission: 'authenticated' + +examples.menu_example.path_only: + path: '/examples/menu_example/path_only' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::pathOnly' + _title: 'Menu_Callback example' + requirements: + _access: 'TRUE' + +examples.menu_example.callback: + path: '/examples/menu_example/path_only/callback' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::routeOnlyRoute' + _title: 'Callback only' + requirements: + _access: 'TRUE' + +examples.menu_example.tabs: + path: 'examples/menu_example/tabs' + defaults: + _content: '\Drupal\menu_example\Controller\MenuExampleController::tabs' + _title: 'Tabs' + requirements: + _access: 'TRUE' + +examples.menu_example.tabname: + path: 'examples/menu_example/tabs/second' + defaults: + _title: 'second' + _content: '\Drupal\menu_example\Controller\MenuExampleController::tabNameSecond' + requirements: + _access: 'TRUE' + +examples.menu_example.tabnameThree: + path: 'examples/menu_example/tabs/third' + defaults: + _title: 'third' + _content: '\Drupal\menu_example\Controller\MenuExampleController::tabNameThrid' + requirements: + _access: 'TRUE' + + examples.menu_example.tabnameFour: + path: 'examples/menu_example/tabs/third' + defaults: + _title: 'four' + _content: '\Drupal\menu_example\Controller\MenuExampleController::tabNameForth' + requirements: + _access: 'TRUE' + +examples.menu_example.defaultfirst: + path: 'examples/menu_example/tabs/default/first' + defaults: + _title: 'Default secondary tab' + requirements: + _access: 'TRUE' + +examples.menu_example.defaultSecond: + path: 'examples/menu_example/tabs/default/Second' + defaults: + _title: 'Default secondary tab' + _content: '\Drupal\menu_example\Controller\MenuExampleController::defaultSecond' + requirements: + _access: 'TRUE' + +examples.menu_example.defaultfirst: + path: 'examples/menu_example/tabs/default/third' + defaults: + _title: 'Default secondary tab' + _content: '\Drupal\menu_example\Controller\MenuExampleController::defaultThird' + requirements: + _access: 'TRUE' + +example.menu_example.use_url_arguments: + path: 'examples/menu_example/use_url_arguments' + defaults: + _title: 'Extra Agruments' + _content: '\Drupal\menu_example\Controller\MenuExampleController::urlArgument' + requirements: + _access: 'TRUE' + + examples.menu_example.title_callbacks: + path: 'examples/menu_example/title_callbacks' + defaults: + _title: 'Drupal\menu_example\Controller\MenuExampleController::backTitle' + _content: '\Drupal\menu_example\Controller\MenuExampleController::titleCallback' + requirements: + _access: 'TRUE' + +examples.menu_example.placeholder_argument: + path: 'examples/menu_example/placeholder_argument' + defaults: + _title: 'Placeholder Arguments' + _content: '\Drupal\menu_example\Controller\MenuExampleController::argument' + requirements: + _access: 'TRUE' + +examples.menu_example.placeholder_argument.display: + path: 'examples/menu_example/placeholder_argument/{node}/display' + defaults: + _title: 'Placeholder Arguments' + _content: '\Drupal\menu_example\Controller\MenuExampleController::display' + requirements: + _access: 'TRUE' + +examples.menu_example.default_arg.arg_optional: + path: 'examples/menu_example/default_arg/{menu_example_arg_optional}' + defaults: + _title: 'Processed Placeholder Arguments' + _content: '\Drupal\menu_example\Controller\MenuExampleController::argOptional' + upcast: '' + requirements: + _access: 'TRUE' + +example.menu_example.menu_original_path: + path: 'examples/menu_example/menu_original_path' + defaults: + _title: 'Menu path that will be altered by hook_menu_alter()' + _content: '\Drupal\menu_example\Controller\MenuExampleController::orginalPath' + requirements: + _access: 'TRUE' diff --git a/menu_example/src/Controller/MenuExampleController.php b/menu_example/src/Controller/MenuExampleController.php new file mode 100644 index 0000000..a969094 --- /dev/null +++ b/menu_example/src/Controller/MenuExampleController.php @@ -0,0 +1,154 @@ +<visit a similar page with no menu link. Also, + note that there is a hook_menu_alter() example that has changed the path + of one of the menu items.', + array('!link' => url('examples/menu_example/path_only')))); + + $base_content = t( + 'This is the base page of the Menu Example. There are a number of examples + here, from the most basic (like this one) to extravagant mappings of loaded + placeholder arguments. Enjoy!'); + return '
' . $base_content . '

' . $content . '
'; +} + +function alternateMenu(){ + return array( + '#markup' => t('This will be in the Main menu instead of the default Tools menu'), + ); +} +public function permissioned() { + return array( + '#markup' => t('A menu item that requires the "access protected menu example" permission is at examples/menu_example/permissioned/controlled', + array('!link' => url('examples/menu_example/permissioned/controlled'))), + ); + } + + public function permissionedControlled() { + return array( + '#markup' => t('This menu entry will not show and the page will not be + accessible without the "access protected menu example" permission.'), + ); + } + + public function customAccess() { + return array( + '#markup' => t('A menu item that requires the user to posess a role of "authenticated" is at examples/menu_example/custom_access/page', array('!link' => url('examples/menu_example/custom_access/page'))), + ); + } + + public function customAccessPage() { + return array( + '#markup' => 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 callback.'), + ); + } + + public function routeOnly() { + return array( + '#markup' => t('A route with no menu link is at !link', array('!link' => url('examples/menu_example/route_only/route'))), + ); + } + + public function routeOnlyRoute() { + return array( + '#markup' => t('The route entry has no corresponding menu_link entry, so it provides a route without a menu link, but it is the same in every other way to the simplest example.'), + ); + } + public function tabs() { + return array( + '#markup' => t('This is the "tabs" menu entry.'), + ); + + } + public function tabNameSecond() { + return array( + '#markup' => t('This is the tab second in the "basic tabs" example', array(second))), + ); + + } + public function tabNameThrid() { + return array( + '#markup' => t('This is the thrid in the "basic tabs" example', array(thrid))), + ); + + } + public function tabNameForth() { + return array( + '#markup' => t('This is the tab forth in the "basic tabs" example', array(forth))), + ); + + } + + public function defaultSecond(){ + + return array(t('This is the secondary tab second in the "basic tabs" example "default" tab', array(second))); + } + + public function defaultThird(){ + + return array(t('This is the secondary tab third in the "basic tabs" example "default" tab', array(third))); + } + + public function urlArgument(){ + return array( + '#markup' => t('This page demonstrates using arguments in the path (portions of the + path after "menu_example/url_arguments". For example, access it with + !link1 or !link2).', + array('!link1' => url('examples/menu_example/use_url_arguments/one/two'), + '!link2' => url('examples/menu_example/use_url_arguments/firstarg/secondarg'))); + ) + } + + public function titleCallbacks() { + return array( + '#markup' => t('The title of this page is dynamically changed by the + title callback for this route. Also, the title of the menu link is dynamically + changed by implementing hook_menu_link_presave() and hook_translated_menu_link_alter().'), + ); + } + + public function customTitle($title = '') { + global $user; + $title .= $user->name; + return $title; + } + + public function argument() { + return array( + '#markup' => t('Demonstrate placeholders by visiting + examples/menu_example/placeholder_argument/3343/display', + array('!link' => url('examples/menu_example/placeholder_argument/3343/display'))), + ); + } + public function display($node = '') { + return array( + '#markup' => $node, + ); + } + public function upcastExample($upcast = '') { + return array( + '#markup' => t('This is a placeholder for now, because it is not working: '. $upcast), + ); + } + + public function orginalPath() { + return array( + '#markup' => t('This menu item was created strictly to allow the + hook_menu_alter() function to have something to operate on. + hook_menu defined the path as examples/menu_example/menu_original_path. + The hook_menu_alter() changes it to examples/menu_example/menu_altered_path. + You can try navigating to both paths and see what happens!') + ); + } + +}