/** * Processes entity route parameters for a given menu link. * * @param \Drupal\menu_link_config\MenuLinkConfigInterface|\Drupal\Core\Menu\MenuLinkInterface $menu_link * The menu link to process. This is being passed in to support * static::postLoad() * @param callable $processor * An entity route parameter processor that gets the entity type ID and the * current route parameter value as arguments and can return the processed * route parameter value or NULL if it does not want to alter the value. */ public static function processEntityRouteParameters($menu_link, $processor) { /** @var \Symfony\Component\Routing\Route $route */ $route = \Drupal::service('router.route_provider')->getRouteByName($menu_link->getRouteName()); $route_parameters = $menu_link->getRouteParameters(); $changed = FALSE; foreach ($route_parameters as $name => $value) { $parameter_info = $route->getOption('parameters'); // Ignore route parameters that are not entity IDs. if (isset($parameter_info[$name]['type']) && strpos($parameter_info[$name]['type'], 'entity:') === 0) { $entity_type_id = substr($parameter_info[$name]['type'], 7); $new_value = $processor($entity_type_id, $value); if (isset($new_value)) { $route_parameters[$name] = $new_value; $changed = TRUE; } } } if ($changed) { $menu_link->set('route_parameters', $route_parameters); } }