diff --git a/menu_example/menu_example.info.yml b/menu_example/menu_example.info.yml new file mode 100644 index 0000000..e9f6e4e --- /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 modules' +core: 8.x +dependencies: + - drupal: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..e4a9c3f --- /dev/null +++ b/menu_example/menu_example.links.task.yml @@ -0,0 +1,37 @@ +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.tabnameThree + title: 'three' + parent_id: examples.menu_example.tabs + weight: 3 + +examples.menu_example.tabThree: + route_name: examples.menu_example.tabnameFour + title: 'four' + parent_id: examples.menu_example.tabs + weight: 4 + +examples.menu_example.secondaryDefault: + route_name: examples.menu_example.default + title: 'Default Secondary Tab' + parent_id: examples.menu_example.default + +examples.menu_example.secondaryDefault: + route_name: examples.menu_example.defaultSecond + title: 'Second Secondary Tab' + parent_id: examples.menu_example.default + +examples.menu_example.secondaryDefault: + route_name: examples.menu_example.defaultThird + title: 'Third Secondary Tab' + parent_id: examples.menu_example.default diff --git a/menu_example/menu_example.module b/menu_example/menu_example.module new file mode 100644 index 0000000..6b639f1 --- /dev/null +++ b/menu_example/menu_example.module @@ -0,0 +1,6 @@ +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]), + ]; + } + + /** + * + */ + public function alternateMenu() { + return [ + '#markup' => t('This will be in the Main menu instead of the default Tools menu'), + ]; + + } + + /** + * + */ + public function permissioned() { + $url = Url::fromUri('internal:/examples/menu_example/permissioned/controlled'); + $link = Link::fromTextAndUrl($this->t('examples/menu_example/permissioned/controlled'), $url)->toString(); + return [ + '#markup' => $this->t('A menu item that requires the "access protected menu example" permission is at @link', ['@link' => $link]), + ]; + } + + /** + * + */ + 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.'), + ]; + } + + /** + * + */ + 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(); + return [ + '#markup' => $this->t('A menu item that requires the user to posess a role of "authenticated" is at @link', ['@link' => $link]), + ]; + } + + /** + * + */ + 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(); + return [ + '#markup' => $this->t('A menu entry with no menu link (MENU_CALLBACK) is at @link', ['@link' => $link]), + ]; + } + + /** + * + */ + public function tabs() { + return [ + '#markup' => $this->t('This is the "tabs" menu entry.'), + ]; + } + + /** + * + */ + public function tabNameSecond() { + return [ + '#markup' => $this->t('This is the tab "Second" in the "basic tabs" example'), + ]; + } + + /** + * + */ + public function tabNameThird() { + return [ + '#markup' => $this->t('This is the tab "Thrid" in the "basic tabs" example'), + ]; + } + + /** + * + */ + public function tabNameForth() { + return [ + '#markup' => $this->t('This is the tab "Forth" in the "basic tabs" example'), + ]; + } + + /** + * + */ + public function defaultSecond() { + return [ + '#markup' => $this->t('This is the secondary tab second in the "basic tabs" example "default"tab'), + ]; + } + + /** + * + */ + public function defaultThird() { + return [ + '#markup' => $this->t('This is the secondary tab Third in the "basic tabs" example "default"tab'), + ]; + } + + /** + * + */ + 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(); + $urle = Url::fromUri('internal:/examples/menu_example/use-url-arguments/one/two'); + $linker = Link::fromTextAndUrl($this->t('examples/menu_example/use-url-arguments/one/two'), $urle)->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 @linker', ['@link' => $link], ['@linker' => $linker]), + + ]; + } + + /** + * + */ + public function titleCallback() { + return [ + '#markup' => $this->t('The title of this page is dynamically changed by the title callback for this route.'), + ]; + } + + /** + * + */ + public function backTitle($title = '') { + // Pass your uid. + $title = \Drupal::currentUser()->id(); + // $title = $account->getUsername()->toString(); + // $account = \Drupal::currentUser()->id(); + // $title=$account->get('name')->value; + return [ + '#markup' => $this->t('@title', ['@title' => $title]), + ]; + } + + /** + * + */ + public function argument() { + $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(); + return [ + '#markup' => $this->t('Demonstrate placeholders by visiting @link', ['@link' => $link]), + ]; + } + + /** + * + */ + public function display($node = '') { + return [ + '#markup' => $node, + ]; + + } + + /** + * + + public function argOptional($no = '') { + if (empty($no) || $arg == '%') ? 99 : $arg; + $mapped_value = NULL; + static $mappings = [ + 1 => 'one', + 2 => 'two', + 3 => 'three', + 99 => 'jackpot! default', + ]; + if (isset($mappings[$id])) { + $mapped_value = $mappings[$no]; + } + $mapped_value = _menu_example_mappings($no); + if (!empty($mapped_value)) { + return [ + '#markup' => $this->t('Loaded value was @loaded', ['@loaded' => $mapped_value]), + ]; + } + else { + return [ + '#markup' => $this->t('Sorry, the id @id was not found to be loaded', ['@id' => $no]), + ]; + } + + }*/ + + /** + * + */ + public function orginalPath() { + return [ + '#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!' + ]; + } +}