Drupal - What is the equivalent of hook_menu_alter() to change a menu item type?
Drupal 8 has a new menu system, there is no hook_menu
and no hook_menu_alter
anymore.
If you want to alter an existing route, it is a little bit more complicated in comparison to Drupal 7.
In your module you have to create a class file at YOURMODULE/src/Routing/CLASSNAME.php
that extends RouteSubscriberBase
:
/**
* @file
* Contains \Drupal\YOURMODULE\Routing\RouteSubscriber.
*/
namespace Drupal\YOURMODULE\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
// Get the route you want to alter
$route = $collection->get('system.admin_content');
// alter the route...
}
}
You can take the RouteSubsciber class of the node module as an example.
To let your RouteSubscriber be recognized you also have to create a YOURMODULE.services.yml
file in the root of your modules directory:
services:
node.route_subscriber:
class: Drupal\YOURMODULE\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
To get a better insight to the new menu system I would recommend the following articles:
- D7 to D8 upgrade tutorial: Convert hook_menu() and hook_menu_alter() to Drupal 8 APIs
- What Happened to Hook_Menu in Drupal 8?
Edit: As mentioned by Berdir, the menu system has a different structure now, which has nothing to do with D7's menu system, so there is no such thing as a menu type anymore.
While the answer from Linus is great, it doesn't provide feedback on your specific question:
Similarly I want to change the menu type in Drupal 8
There is no such thing as a menu type in Drupal 8. Everything that used to be a type is now a completely different thing. Routes, Menu Links, Local Tasks, Local Actions. And often, you have multiple things. You always have a route (7.x used to call this type callback). And addition, you add menu links, local tasks or actions for that route.
So, you can't convert a menu link to a local task for example, or even a route to a local task. The only thing you could do is e.g. alter a menu link away (which has nothing to do with route alter mentioned above) and create a new local task instead.
See Linus' answer for links and more information on how to do each of those things.