13

I want to change the menu type in Drupal 8. In Drupal 7 we can achieve this using hook_menu_alter

function module_menu_alter(&$items) {
  $items['admin/config/people/ip-blocking/default'] = array(
    'title' => 'Block IP Address',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
}

Similarly I want to change the menu type in Drupal 8. How can I do it. Thank You.

2
  • 3
    read it drupal.org/node/2118147
    – Adi
    Jan 21, 2016 at 8:26
  • As mentioned by Berdir, there is no menu type anymore. Can you maybe specify what you are trying to archive?
    – Linus
    Jan 22, 2016 at 8:14

2 Answers 2

8

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.

1
  • You're right, I totally forgot to mention that. Thanks for the addition.
    – Linus
    Jan 22, 2016 at 8:16
22

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:

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.