You are here

function og_menu_system_breadcrumb_alter in Organic Groups Menu (OG Menu) 8

Implements hook_system_breadcrumb_alter().

File

./og_menu.module, line 112
Main functions and hook implementations of the og_menu module.

Code

function og_menu_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
  if ($route_match
    ->getRouteName() === 'menu_ui.link_edit' && ($menu_link = $route_match
    ->getParameter('menu_link_plugin'))) {
    if ($menu_link instanceof MenuLinkInterface) {
      $menu_name = $menu_link
        ->getMenuName();
      $menu = Menu::load($menu_name);

      // If the menu is not a normal menu, check whether it's prefixed by
      // "ogmenu-" and see if we can load an ogmenu_instance instead.
      if (!$menu && strpos($menu_name, 'ogmenu-') === 0) {
        $og_menu_instance_id = str_replace('ogmenu-', '', $menu_name);
        $menu = \Drupal::entityTypeManager()
          ->getStorage('ogmenu_instance')
          ->load($og_menu_instance_id);
      }

      // Depending on whether the menu is an ogmenu_instance or not we should
      // use a different route.
      if ($menu instanceof OgMenuInstance) {
        $breadcrumb
          ->addLink(Link::createFromRoute($menu
          ->label(), 'entity.ogmenu_instance.edit_form', [
          'ogmenu_instance' => $menu
            ->id(),
        ]));
      }
      else {
        $breadcrumb
          ->addLink(Link::createFromRoute($menu
          ->label(), 'entity.menu.edit_form', [
          'menu' => $menu
            ->id(),
        ]));
      }
    }
  }
}