I am trying to restrict access for non admin users on structure->menues so that they can’t create any new links there.
Now they asked me if for the special menu "Focus" they could have the "Add Link" feature re enabled. I can get the menu->id() in the module file but somehow not in the routesubscriber. Drush CR returns an
PHP Fatal error: Uncaught Error: Call to a member function id() on null in /var/www/html/web/modules/custom/my_menu/src/Routing/RouteSubscriber.php:31 Stack trace: #0 /var/www/html/web/core/lib/Drupal/Core/Routing/RouteSubscriberBase.php(37): Drupalhw_menuRoutingRouteSubscriber->alterRoutes(Object(SymfonyComponentRoutingRouteCollection)) #1 [internal function]: DrupalCoreRoutingRouteSubscriberBase->onAlterRoutes(Object(DrupalCoreRoutingRouteBuildEvent), 'routing.route_a...', Object(DrupalComponentEventDispatcherContainerAwareEventDispatcher)) #2 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(142): call_user_func(Array, Object(DrupalCoreRoutingRouteBuildEvent), 'routing.route_a...', Object(DrupalComponentEventDispatcherContainerAwareEventDispatcher)) #3 /var/www/html/web/core/lib/Drupal/Core/Routing/RouteBuilder.php(189): DrupalComponentEventDispatcherContainerAwareEventDispatcher->dispatch(Object(DrupalCoreRoutingRouteBuildEvent), 'routing.route_a...') in /var/www/html/web/modules/custom/my_menu/src/Routing/RouteSubscriber.php on line 31
Are there other ways to get the value?
Here is my code.
<?php namespace Drupalmy_menuRouting; use DrupalCoreRoutingRouteSubscriberBase; use DrupalCoreRoutingRoutingEvents; use SymfonyComponentRoutingRouteCollection; use DrupalCoreRoutingRouteMatchInterface; /** * Class RouteSubscriber * * hiding menu editing components for non-admins * * @package Drupalhw_menuRouting */ class RouteSubscriber extends RouteSubscriberBase { /** * {@inheritdoc} */ public function alterRoutes(RouteCollection $collection) { if ($route = $collection->get('entity.menu.add_link_form')) { // dpm($route->getRequirements()); $menu instanceof DrupalsystemEntityMenu; $menu = Drupal::routeMatch()->getParameter('menu'); if($menu->id() != 'focus') { $route->setRequirement('_role', 'administrator'); } // dpm($route->getRequirements()); } } /** * {@inheritdoc} */ public static function getSubscribedEvents() { // Run after content_translation, which has priority -210. $events[RoutingEvents::ALTER] = ['onAlterRoutes', -230]; return $events; } }