function Link::__construct

Same name in this branch
  1. 9 core/lib/Drupal/Core/Link.php \Drupal\Core\Link::__construct()
Same name in other branches
  1. 8.9.x core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link::__construct()
  2. 8.9.x core/lib/Drupal/Core/Link.php \Drupal\Core\Link::__construct()
  3. 10 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link::__construct()
  4. 10 core/lib/Drupal/Core/Link.php \Drupal\Core\Link::__construct()
  5. 11.x core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link::__construct()
  6. 11.x core/lib/Drupal/Core/Link.php \Drupal\Core\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));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.