function Link::__construct

Same name in this branch
  1. 8.9.x core/lib/Drupal/Core/Link.php \Drupal\Core\Link::__construct()
Same name in other branches
  1. 9 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link::__construct()
  2. 9 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, $link_relation_type, array $target_attributes = []) {
    // @todo Remove this conditional block in drupal:9.0.0 and add a type hint to the $link_relation_type argument of this method in https://www.drupal.org/project/drupal/issues/3080467.
    if (is_array($link_relation_type)) {
        @trigger_error('Constructing a ' . self::class . ' with an array of link relation types is deprecated in drupal:8.8.0 and will throw a fatal error in drupal:9.0.0. Pass a single string instead. See https://www.drupal.org/node/3087821.', E_USER_DEPRECATED);
        assert(Inspector::assertAllStrings($link_relation_type));
    }
    else {
        assert(is_string($link_relation_type));
        $link_relation_type = [
            $link_relation_type,
        ];
    }
    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.