You are here

protected function MenuItemsResource::getMenuItems in JSON:API Menu Items 1.1.x

Same name and namespace in other branches
  1. 1.2.x src/Resource/MenuItemsResource.php \Drupal\jsonapi_menu_items\Resource\MenuItemsResource::getMenuItems()
  2. 1.0.x src/Resource/MenuItemsResource.php \Drupal\jsonapi_menu_items\Resource\MenuItemsResource::getMenuItems()

Generate the menu items.

Parameters

array $tree: The menu tree.

array $items: The already created items.

\Drupal\Core\Cache\CacheableMetadata $cache: The cacheable metadata.

1 call to MenuItemsResource::getMenuItems()
MenuItemsResource::process in src/Resource/MenuItemsResource.php
Process the resource request.

File

src/Resource/MenuItemsResource.php, line 105

Class

MenuItemsResource
Processes a request for a collection of featured nodes.

Namespace

Drupal\jsonapi_menu_items\Resource

Code

protected function getMenuItems(array $tree, array &$items, CacheableMetadata $cache) {
  foreach ($tree as $menu_link) {
    if ($menu_link->access !== NULL && !$menu_link->access instanceof AccessResultInterface) {
      throw new \DomainException('MenuLinkTreeElement::access must be either NULL or an AccessResultInterface object.');
    }
    if ($menu_link->access instanceof AccessResultInterface) {
      $cache
        ->merge(CacheableMetadata::createFromObject($menu_link->access));
    }

    // Only return accessible links.
    if ($menu_link->access instanceof AccessResultInterface && !$menu_link->access
      ->isAllowed()) {
      continue;
    }
    $id = $menu_link->link
      ->getPluginId();
    [
      $plugin,
    ] = explode(':', $id);
    switch ($plugin) {
      case 'menu_link_content':
      case 'menu_link_config':
        $resource_type = $this->resourceTypeRepository
          ->get($plugin, $plugin);
        break;
      default:

        // @todo Use a custom resource type?
        $resource_type = $this->resourceTypeRepository
          ->get('menu_link_content', 'menu_link_content');
    }
    $url = $menu_link->link
      ->getUrlObject()
      ->toString(TRUE);
    assert($url instanceof GeneratedUrl);
    $cache
      ->addCacheableDependency($url);
    $fields = [
      'description' => $menu_link->link
        ->getDescription(),
      'enabled' => $menu_link->link
        ->isEnabled(),
      'expanded' => $menu_link->link
        ->isExpanded(),
      'menu_name' => $menu_link->link
        ->getMenuName(),
      'meta' => $menu_link->link
        ->getMetaData(),
      'options' => $menu_link->link
        ->getOptions(),
      'parent' => $menu_link->link
        ->getParent(),
      'provider' => $menu_link->link
        ->getProvider(),
      'route' => [
        'name' => $menu_link->link
          ->getRouteName(),
        'parameters' => $menu_link->link
          ->getRouteParameters(),
      ],
      'title' => (string) $menu_link->link
        ->getTitle(),
      'url' => $url
        ->getGeneratedUrl(),
      'weight' => $menu_link->link
        ->getWeight(),
    ];
    $links = new LinkCollection([]);
    $resource_object_cacheability = new CacheableMetadata();
    $resource_object_cacheability
      ->addCacheableDependency($menu_link->access);
    $resource_object_cacheability
      ->addCacheableDependency($cache);
    $items[$id] = new ResourceObject($resource_object_cacheability, $resource_type, $id, NULL, $fields, $links);
    if ($menu_link->subtree) {
      $this
        ->getMenuItems($menu_link->subtree, $items, $cache);
    }
  }
}