You are here

public function MenuChildren::getMenuLinkFromTargetUrl in Views Menu Node Children Filter 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/views/argument/MenuChildren.php \Drupal\views_menu_children_filter\Plugin\views\argument\MenuChildren::getMenuLinkFromTargetUrl()
  2. 3.0.x src/Plugin/views/argument/MenuChildren.php \Drupal\views_menu_children_filter\Plugin\views\argument\MenuChildren::getMenuLinkFromTargetUrl()

Takes a path (url) and finds the matching MenuLink within the provided menus.

Parameters

array|NULL $menus The menus to search for the parent within:

Url $url The argument, usually a path or a NID:

bool $reset_cache Resets the static caching.:

Return value

\Drupal\Core\Menu\MenuLinkInterface|null

1 call to MenuChildren::getMenuLinkFromTargetUrl()
MenuChildren::filterChildrenNodeByParent in src/Plugin/views/argument/MenuChildren.php
Filter results to child nodes of a MenuLink found by the $parent_page_identifier.

File

src/Plugin/views/argument/MenuChildren.php, line 136

Class

MenuChildren
A filter to show menu children of a parent menu item

Namespace

Drupal\views_menu_children_filter\Plugin\views\argument

Code

public function getMenuLinkFromTargetUrl($menus, Url $url = NULL, $reset_cache = FALSE) {
  static $route_links = [];
  $route = NULL;
  $target_menu_link = NULL;
  if ($reset_cache) {
    $route_links = [];
  }
  if (empty($menus)) {
    $menus = [
      'all_menus',
    ];
  }
  if (isset($url)) {
    $route = $url
      ->getRouteName();
    $routeParameters = $url
      ->getRouteParameters();

    // There is a strange behavior in looking up a link by route if a route
    // parameter only has a key, and no value. Check of the route parameters
    // has any keys with null values.
    foreach ($routeParameters as $key => $value) {
      if (is_null($value)) {
        unset($routeParameters[$key]);
      }
    }
    foreach ((array) $menus as $menu) {

      // Build an identifier of our values for static caching lookups.
      $path_identifier = self::buildRouteIdentifier($menu, $route, $routeParameters);

      // Has this lookup been performed and cached already?
      if (isset($route_links[$path_identifier])) {
        return $route_links[$path_identifier];
      }
      if ($menu === 'all_menus') {
        $menu = NULL;
      }
      $menu_links = $this->menuLinkManager
        ->loadLinksByRoute($route, $routeParameters, $menu);
      if (!empty($menu_links)) {

        // Ideally, only one result is returned.
        // If multiple links returned, maybe I should add some kind of reporting?

        //TODO: Add alerting, throw exception, or log the fact that more than one result was found.
        $target_menu_link = reset($menu_links);
        $route_links[$path_identifier] = $target_menu_link;
        break;
      }
    }
  }
  return $target_menu_link;
}