You are here

public function Link::__construct in Drupal 9

Same name in this branch
  1. 9 core/lib/Drupal/Core/Link.php \Drupal\Core\Link::__construct()
  2. 9 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link::__construct()
Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link::__construct()

JSON:API Link constructor.

Parameters

\Drupal\Core\Cache\CacheableMetadata $cacheability: Any cacheability metadata associated with the link. For example, a 'call-to-action' link might reference a registration resource if an event has vacancies or a wait-list resource otherwise. Therefore, the link's cacheability might be depend on a certain entity's values other than the entity on which the link will appear.

\Drupal\Core\Url $url: The Url object for the link.

string $link_relation_type: An array of registered or extension RFC8288 link relation types.

array $target_attributes: An associative array of target attributes for the link.

See also

https://tools.ietf.org/html/rfc8288#section-2.1

File

core/modules/jsonapi/src/JsonApiResource/Link.php, line 78

Class

Link
Represents an RFC8288 based link.

Namespace

Drupal\jsonapi\JsonApiResource

Code

public function __construct(CacheableMetadata $cacheability, Url $url, string $link_relation_type, array $target_attributes = []) {
  assert(Inspector::assertAllStrings(array_keys($target_attributes)));
  assert(Inspector::assertAll(function ($target_attribute_value) {
    return is_string($target_attribute_value) || is_array($target_attribute_value);
  }, array_values($target_attributes)));
  $generated_url = $url
    ->setAbsolute()
    ->toString(TRUE);
  $this->href = $generated_url
    ->getGeneratedUrl();
  $this->uri = $url;
  $this->rel = $link_relation_type;
  $this->attributes = $target_attributes;
  $this
    ->setCacheability($cacheability
    ->addCacheableDependency($generated_url));
}