Skip to main content

Drupal ^8: How to activate a menu item based on a url with query parameters

Recently i discovered that when you render a custom menu on a sidebar and you have a custom pager inside a menu item the active trail is broken. For example let's say we have this menu :

  • Home
  • About
  • Services

and services is a custom dynamic controller index page where you can go forward and backward like this /services?item=1 ,/services?item=2 , /services?item=3 when you are on the default page it is ok the active trail is shown as "active" but when you are at /services?item=1 it wont show as active. This is how you can make it active when you are on the same path with query parameters at the same url.

function atheme_preprocess_menu__booking_manager(&$variables, $hook) {
  //Custom menus that needs to be active
  $in_hook = ['menu__booking_manager', 'menu__attraction_manager', 'menu__general_admin'];

  $items = $variables['items'];
  if (!in_array($hook, $in_hook)) {
    return $variables;

  foreach ($items as $key => $item) {
    $in_path = $item['url']->getInternalPath();
    if (strpos($current_path, 'services') !== false && strpos($in_path, 'services') !== false) {
     //If this is a services?item=1 page set it active too
      $variables['items'][$key]['in_active_trail'] = TRUE;


custom module php