protected function MenuItemsResource::getMenuItems in JSON:API Menu Items 1.0.x
Same name and namespace in other branches
- 1.2.x src/Resource/MenuItemsResource.php \Drupal\jsonapi_menu_items\Resource\MenuItemsResource::getMenuItems()
- 1.1.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 104
Class
- MenuItemsResource
- Processes a request for a collection of featured nodes.
Namespace
Drupal\jsonapi_menu_items\ResourceCode
protected function getMenuItems(array $tree, array &$items, CacheableMetadata $cache) {
foreach ($tree as $menu_link) {
$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(),
],
// @todo Don't cast this to string once we've resolved
// https://www.drupal.org/project/jsonapi_menu_items/issues/3171184
'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);
}
}
}