Check-Menus.com

drupal 8 menu hooks

by Antone Bergstrom II Published 2 years ago Updated 1 year ago
image

In Drupal 8 you can use the hook_link_alter (&$variables) hook to alter menu links. This is what the $variables argument looks like: As you can see, you can modify the link text here. But, there is no easy way to identify this menu link.

Full Answer

How to use hooks in Drupal 8?

How to use hooks in Drupal 8 modules or themes? Hooks are PHP functions that are created for each module when system events happen, e.g. page load or user login. In Drupal 7 custom pages are created using hook_menu. Drupal 8 is yaml-based and you need create controller, routes.yml and basic info file for your module.

What happened to hook_menu in Drupal 8?

What Happened to Hook_Menu in Drupal 8? In Drupal 7 and earlier versions hook_menu has been the Swiss Army knife of hooks. It does a little bit of everything: page paths, menu callbacks, tabs and local tasks, contextual links, access control, arguments and parameters, form callbacks, and on top of all that it even sets up menu items!

How do I alter menu links in Drupal 8?

And, as in some many situations in Drupal -- hooks to the rescue! In Drupal 8 you can use the hook_link_alter (&$variables) hook to alter menu links. This is what the $variables argument looks like:

How to create a custom page in Drupal 8?

In Drupal 7 custom pages are created using hook_menu. Drupal 8 is yaml-based and you need create controller, routes.yml and basic info file for your module. hook_menu () -> Drupal 8 APIs should start with defining the routes for the items.

image

How to invoke a hook in Drupal?

To invoke a hook, use methods on DrupalCoreExtensionModuleHandlerInterface such as alter (), invoke (), and invokeAll ( ). You can obtain a module handler by calling Drupal::moduleHandler (), or getting the 'module_handler' service on an injected container.

How to alter core behavior in Drupal?

One way for modules to alter the core behavior of Drupal (or another module) is to use hooks. Hooks are specially-named functions that a module defines (this is known as "implementing the hook"), which are discovered and called at specific times to alter or add to the base behavior or data (this is known as "invoking the hook").

Where are hooks documented?

Hooks are documented in *.api.php files, by defining functions whose name starts with "hook_" (these files and their functions are never loaded by Drupal -- they exist solely for documentation). The function should have a documentation header, as well as a sample function body.

What does hook_menu_alter mean?

If you used hook_menu_alter () (incorrectly) to define dynamic items, see above for dynamic routes, actions, etc. Altering existing items in the new systems is different based on what you are trying to alter.

Can you create dynamic routes in Drupal 8?

In Drupal 8 terms, this creates several routes and menu items. When converting this to Drupal 8 you may want to port this to create dynamic routes and dynamic menu items. Both are possible. However, routes do not need to be dynamic as it is very simple to create one route that would apply with all menu items.

How to implement hooks in Drupal?

For example, if your module is named 'mymodule' and you're using a form_alter, hook_form_alter becomes mymodule_form_alter.

Does Drupal 8 use Symfony?

hook_init () -> Drupal 8 has adopted symfony as a part of its core. It uses Symfony kernel and events to do the same now.

Where to find menu links yml?

The easiest way is to do a search through the code base. If I do a find for title: ‘Home’ then I’ll find the menu links YAML file at core/profiles/standard/standard. links.menu.yml.

What is the last part of menu?

The last part is the name of the menu, main. We will need this when adding a menu link to this.

image

Paths with Arguments

  • Some paths need additional arguments or parameters. If my page had a couple extra parameters it would look like this in Drupal 7: In Drupal 8 the YAML file would be adjusted to look like this (adding the parameters to the path): The controller then looks like this (showing the parameters …
See more on lullabot.com

Paths with Optional Arguments

  • The above code will work correctly only for that specific path, with both parameters. Neither the path /main, nor /main/first will work, only /main/first/second. If you want the parameters to be optional, so /main, /main/first, and /main/first/secondare all valid paths, you need to make some changes to the YAML file. By adding the arguments to the defaults section you are telling the co…
See more on lullabot.com

Restricting Parameters

  • Once you set up parameters you probably should also provide information about what values will be allowed for them. You can do this by adding some more information to the YAML file. The example below indicates that $first can only contain the values ‘Y’ or ‘N’, and $secondmust be a number. Any parameters that don’t match these rules will return a 404. Basically the code is exp…
See more on lullabot.com

Entity Parameters

  • As in Drupal 7, when creating a route that has an entity id you can set it up so the system will automatically pass the entity object to the callback instead of just the id. This is called ‘upcasting’. In Drupal 7 we did this by using %node instead of %. In Drupal 8 you just need to use the name of the entity type as the parameter name, for instance {node} or {user}. This upcasting only happen…
See more on lullabot.com

Json Callbacks

  • All the above code will create HTML at the specified path. Your render array will be converted to HTML automatically by the system. But what if you wanted that path to display JSON instead? I had trouble finding any documentation about how to do that. There is some old documentation that indicates you need to add _format: json to the YAML file in the requirementssection, but tha…
See more on lullabot.com

Access Control

  • Hook_menu() also manages access control. Access control is now handled by the MODULE.routing.ymlfile. There are various ways to control access: Allow access by anyone to this path: Limit access to users with ‘access content’ permission: Limit access to users with the ‘admin’ role: Limit access to users who have ‘edit’ permission on an entity (when the entity is pro…
See more on lullabot.com

hook_menu_alter

  • So what if a route already exists (created by core or some other module) and you want to alter something about it? In Drupal 7 that is done with hook_menu_alter, but that hook is also removed in Drupal 8. It’s a little more complicated now. The simplest example in core I could find was in the Node module, which is altering a route created by the System module. A class file at MODULE/sr…
See more on lullabot.com

and more!

  • And these are still only some of the things that are done in hook_menu in Drupal 7 that need to be transformed to Drupal 8. Hook_menu is also used for creating menu items, local tasks (tabs), contextual links, and form callbacks. I’ll dive into the Drupal 8 versions of some of those in a later article. More information about this topic: 1. https://api.drupal.org/api/drupal/8 2. https://www.dr…
See more on lullabot.com

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9