You are here

public function MenuBadgesManager::getLocalRoutes in Menu Badges 8

File

src/MenuBadgesManager.php, line 27

Class

MenuBadgesManager

Namespace

Drupal\menu_badges

Code

public function getLocalRoutes($types = array(), $title = NULL, $path = NULL) {
  $routes = array();
  if (empty($types) || in_array(MenuBadgesManager::LOCAL_TASK, $types)) {
    $tabs = \Drupal::service('plugin.manager.menu.local_task')
      ->getDefinitions();
    foreach ($tabs as $id => $info) {
      $info['menu_badges_route_type'] = MenuBadgesManager::LOCAL_TASK;
      $routes[$id] = $info;
    }
  }
  if (empty($types) || in_array(MenuBadgesManager::LOCAL_ACTION, $types)) {
    $actions = \Drupal::service('plugin.manager.menu.local_action')
      ->getDefinitions();
    foreach ($actions as $id => $info) {
      $info['menu_badges_route_type'] = MenuBadgesManager::LOCAL_ACTION;
      $routes[$id] = $info;
    }
  }
  if (!empty($title)) {
    foreach ($routes as $id => $route) {
      if (empty($route['title']) || !stristr($route['title'], $title)) {
        unset($routes[$id]);
      }
    }
  }

  // Get path information and merge.
  $paths = db_select('router', 'r')
    ->fields('r', array(
    'name',
    'path',
  ))
    ->execute()
    ->fetchAllKeyed();
  foreach ($routes as $id => $route) {
    if (!empty($paths[$route['route_name']])) {
      $routes[$id]['menu_badges_route_path'] = $paths[$route['route_name']];
    }
  }
  if (!empty($path)) {
    foreach ($routes as $id => $route) {
      if (empty($route['menu_badges_route_path']) || !stristr($route['menu_badges_route_path'], $path)) {
        unset($routes[$id]);
      }
    }
  }
  return $routes;
}