You are here

public function MenuTreeResource::get in REST Menu Tree 8

Same name and namespace in other branches
  1. 2.x src/Plugin/rest/resource/MenuTreeResource.php \Drupal\rest_menu_tree\Plugin\rest\resource\MenuTreeResource::get()

File

src/Plugin/rest/resource/MenuTreeResource.php, line 83

Class

MenuTreeResource
Provides a resource to get view modes by entity and bundle.

Namespace

Drupal\rest_menu_tree\Plugin\rest\resource

Code

public function get(MenuInterface $menu) {
  $params = new MenuTreeParameters();
  $tree = $this->menuTree
    ->load($menu
    ->id(), $params);

  // Transform the tree using manipulators.
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $this->menuTree
    ->transform($tree, $manipulators);

  // Ensure that every item has an access response, if possible.
  $this
    ->addAccess($tree);

  // Maintain a clean version of the tree for adding cache metadata to the
  // response.
  $clean = $tree;

  // Remove items the user does not have access to.
  $this
    ->checkAccess($tree);

  // Remove the keys to prevent reordering.
  $this
    ->removeKeys($tree);
  $response = new ResourceResponse($tree);

  // First add every item to the cache.
  $response
    ->addCacheableDependency($menu);

  // Always use the clean version of the tree so every link is cached,
  // regardless of whether it's accessible to the current user or not.
  $this
    ->addCacheDependencies($clean, $response);
  return $response;
}