You are here

protected function RestMenuItemsResource::getElementValue in Rest menu items 3.0.x

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

Generate the menu element value.

Parameters

array $returnArray: The return array we want to add this item to.

string $key: The key to use in the output.

\Drupal\Core\Menu\MenuLinkInterface $link: The link from the menu.

\Drupal\Core\Url $url: The URL object of the menu item.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to RestMenuItemsResource::getElementValue()
RestMenuItemsResource::getMenuItems in src/Plugin/rest/resource/RestMenuItemsResource.php
Generate the menu tree we can use in JSON.

File

src/Plugin/rest/resource/RestMenuItemsResource.php, line 248

Class

RestMenuItemsResource
Provides a resource to get bundles by entity.

Namespace

Drupal\rest_menu_items\Plugin\rest\resource

Code

protected function getElementValue(array &$returnArray, $key, MenuLinkInterface $link, Url $url) {
  $config = $this->configFactory
    ->get('rest_menu_items.config');
  $external = $url
    ->isExternal();
  $routed = $url
    ->isRouted();
  $existing = TRUE;
  $value = NULL;

  // Check if the url is a <nolink> and do not do anything for some keys.
  $itemsToRemoveWhenNoLink = [
    'uri',
    'alias',
    'absolute',
    'relative',
  ];
  if (!$external && $routed && $url
    ->getRouteName() === '<nolink>' && in_array($key, $itemsToRemoveWhenNoLink)) {
    return;
  }
  if ($external || !$routed) {
    $uri = $url
      ->getUri();
  }
  else {
    try {
      $uri = $url
        ->getInternalPath();
    } catch (\UnexpectedValueException $e) {
      $uri = $relative = Url::fromUri($url
        ->getUri())
        ->toString();
      $existing = FALSE;
    }
  }
  switch ($key) {
    case 'key':
      $value = $link
        ->getDerivativeId();
      if (empty($value)) {
        $value = $link
          ->getBaseId();
      }
      break;
    case 'title':
      $value = $link
        ->getTitle();
      break;
    case 'description':
      $value = $link
        ->getDescription();
      break;
    case 'uri':
      $value = $uri;
      break;
    case 'alias':
      if ($routed) {
        $value = ltrim($this->aliasManager
          ->getAliasByPath("/{$uri}"), '/');
      }
      break;
    case 'external':
      $value = $external;
      break;
    case 'absolute':
      $base_url = $config
        ->get('base_url');
      if ($external) {
        $value = $uri;
      }
      elseif (!$routed) {
        if (empty($base_url)) {
          $url
            ->setAbsolute();
        }
        $value = $url
          ->toString(TRUE)
          ->getGeneratedUrl();
        if (!empty($base_url)) {
          $value = $base_url . $value;
        }
      }
      else {
        $options = [];
        if (empty($base_url)) {
          $options = [
            'absolute' => TRUE,
          ];
        }
        $value = Url::fromUri('internal:/' . $uri, $options)
          ->toString(TRUE)
          ->getGeneratedUrl();
        if (!empty($base_url)) {
          $value = $base_url . $value;
        }
      }
      break;
    case 'relative':
      if (!$external) {
        $value = Url::fromUri('internal:/' . $uri, [
          'absolute' => FALSE,
        ])
          ->toString(TRUE)
          ->getGeneratedUrl();
      }
      if (!$routed) {
        $url
          ->setAbsolute(FALSE);
        $value = $url
          ->toString(TRUE)
          ->getGeneratedUrl();
      }
      if (!$existing) {
        $value = Url::fromUri($url
          ->getUri())
          ->toString();
      }
      break;
    case 'existing':
      $value = $existing;
      break;
    case 'weight':
      $value = $link
        ->getWeight();
      break;
    case 'expanded':
      $value = $link
        ->isExpanded();
      break;
    case 'enabled':
      $value = $link
        ->isEnabled();
      break;
    case 'uuid':
      if (!$external && $routed) {
        $params = Url::fromUri('internal:/' . $uri)
          ->getRouteParameters();
        $entity_type = key($params);
        if (!empty($entity_type) && $this->entityTypeManager
          ->hasDefinition($entity_type)) {
          $entity = $this->entityTypeManager
            ->getStorage($entity_type)
            ->load($params[$entity_type]);
          $value = $entity
            ->uuid();
        }
      }
      break;
    case 'options':
      $value = $link
        ->getOptions();
      break;
  }
  $addFragmentElements = [
    'alias',
    'absolute',
    'relative',
  ];
  if (!empty($config
    ->get('add_fragment')) && in_array($key, $addFragmentElements)) {
    $this
      ->addFragment($value, $link);
  }
  $returnArray[$key] = $value;
}