You are here

protected function AccessRestrictedLink::__construct in JSON:API Hypermedia 8

AccessRestrictedLink constructor.

Parameters

\Drupal\Core\Access\AccessResultInterface $access_result: An access result.

\Drupal\Core\Cache\CacheableDependencyInterface $link_cacheability: (optional) Cacheability of the generated link.

\Drupal\Core\Url $target: (optional) The link URL.

string $link_relation_type: (optional) The link's relation type.

array $target_attributes: (optional) The link's target attributes.

\Drupal\Core\Url|null $context: (optional) The link's context. NULL if the default context shouldn't be overridden.

File

src/AccessRestrictedLink.php, line 53

Class

AccessRestrictedLink
Decorates a JSON:API link to consider link accessibility.

Namespace

Drupal\jsonapi_hypermedia

Code

protected function __construct(AccessResultInterface $access_result, CacheableDependencyInterface $link_cacheability = NULL, Url $target = NULL, $link_relation_type = '', array $target_attributes = [], $context = NULL) {

  // Most arguments can be omitted if the link is inaccessible.
  assert(!$access_result
    ->isAllowed() || $link_cacheability && $target && !empty($link_relation_type));
  assert(is_null($context) || $context instanceof Url);
  $this
    ->setCacheability(CacheableMetadata::createFromObject($access_result));
  $this->accessResult = $access_result;
  if ($access_result
    ->isAllowed()) {
    $cacheable_metadata = CacheableMetadata::createFromObject($link_cacheability);
    if ($context) {
      $anchor_href = $context
        ->setAbsolute()
        ->toString(TRUE);
      $cacheable_metadata
        ->addCacheableDependency($anchor_href);
      $target_attributes['anchor'] = $anchor_href
        ->getGeneratedUrl();
    }
    $this->inner = new DecoratedLink($cacheable_metadata, $target, $link_relation_type, $target_attributes);
  }
}