You are here

protected function RestMenuItemsResource::getMenuItems in Rest menu items 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/rest/resource/RestMenuItemsResource.php \Drupal\rest_menu_items\Plugin\rest\resource\RestMenuItemsResource::getMenuItems()
  2. 3.0.x src/Plugin/rest/resource/RestMenuItemsResource.php \Drupal\rest_menu_items\Plugin\rest\resource\RestMenuItemsResource::getMenuItems()

Generate the menu tree we can use in JSON.

Parameters

array $tree: The menu tree.

array $items: The already created items.

1 call to RestMenuItemsResource::getMenuItems()
RestMenuItemsResource::get in src/Plugin/rest/resource/RestMenuItemsResource.php
Responds to GET requests.

File

src/Plugin/rest/resource/RestMenuItemsResource.php, line 195
Create the menu item REST resource.

Class

RestMenuItemsResource
Provides a resource to get bundles by entity.

Namespace

Drupal\rest_menu_items\Plugin\rest\resource

Code

protected function getMenuItems(array $tree, array &$items = []) {
  foreach ($tree as $item_value) {

    /* @var $org_link \Drupal\Core\Menu\MenuLinkDefault */
    $org_link = $item_value['original_link'];
    $options = $org_link
      ->getOptions();

    // Set name to uuid or base id.
    $item_name = $org_link
      ->getDerivativeId();
    if (empty($item_name)) {
      $item_name = $org_link
        ->getBaseId();
    }

    /* @var $url \Drupal\Core\Url */
    $url = $item_value['url'];
    $existing = TRUE;
    $external = $url
      ->isExternal();
    $uuid = '';
    if ($external) {
      $uri = $url
        ->getUri();
      $absolute = $uri;
      $relative = NULL;
    }
    else {
      try {
        $uri = $url
          ->getInternalPath();
        $absolute = Url::fromUri('internal:/' . $uri, [
          'absolute' => TRUE,
        ])
          ->toString(TRUE)
          ->getGeneratedUrl();
        $relative = Url::fromUri('internal:/' . $uri, [
          'absolute' => FALSE,
        ])
          ->toString(TRUE)
          ->getGeneratedUrl();
        $params = Url::fromUri('internal:/' . $uri)
          ->getRouteParameters();
        $entity_type = key($params);
        if (!empty($entity_type)) {
          $entity = \Drupal::entityTypeManager()
            ->getStorage($entity_type)
            ->load($params[$entity_type]);
          $uuid = $entity
            ->uuid();
        }
      } catch (\UnexpectedValueException $e) {
        $absolute = '';
        $uri = $relative = Url::fromUri($url
          ->getUri())
          ->toString();
        $existing = FALSE;
      }
    }
    $alias = $this->aliasManager
      ->getAliasByPath("/{$uri}");
    $value = $value = [
      'key' => $item_name,
      'title' => $org_link
        ->getTitle(),
      'description' => $org_link
        ->getDescription(),
      'uri' => $uri,
      'alias' => ltrim($alias, '/'),
      'external' => $external,
      'absolute' => $absolute,
      'relative' => $relative,
      'existing' => $existing,
      'weight' => $org_link
        ->getWeight(),
      'expanded' => $org_link
        ->isExpanded(),
      'enabled' => $org_link
        ->isEnabled(),
      'uuid' => $uuid,
      'options' => $options,
    ];
    if (!empty($item_value['below'])) {
      $value['below'] = [];
      $this
        ->getMenuItems($item_value['below'], $value['below']);
    }
    $items[] = $value;
  }
}