You are here

public function TestLinkProvider::getLink in JSON:API Hypermedia 8

Adds, alters or removes hyperlinks from a link collection.

Parameters

\Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel|\Drupal\jsonapi\JsonApiResource\ResourceObject|\Drupal\jsonapi\JsonApiResource\Relationship $context: The context object from which links should be generated.

Return value

\Drupal\jsonapi_hypermedia\AccessRestrictedLink A link to be added to the context object. An AccessRestrictedLink should be returned if the link target may be inaccessible to some users.

Overrides LinkProviderInterface::getLink

File

tests/modules/jsonapi_hypermedia_test_link_providers/src/Plugin/jsonapi_hypermedia/LinkProvider/TestLinkProvider.php, line 56

Class

TestLinkProvider
Class TestLinkProvider.

Namespace

Drupal\jsonapi_hypermedia_test_link_providers\Plugin\jsonapi_hypermedia\LinkProvider

Code

public function getLink($context) {
  $plugin_definition = $this
    ->getPluginDefinition();
  $link_key = $this
    ->getLinkKey();
  $url = Url::fromUri("https://drupal.org/project/jsonapi_hypermedia/{$link_key}");
  $random = (new Random())
    ->name();
  $attributes = array_merge($plugin_definition['_test_target_attributes'] ?? [], [
    'randomAttr' => $random,
  ]);
  $access_restricted = !empty($plugin_definition['_test_restrict_access']);
  $cacheability = new CacheableMetadata();
  $cacheability
    ->addCacheTags([
    'test_jsonapi_hypermedia_cache_tag',
  ]);
  $permission = "view {$this->getLinkKey()} link";
  $access_result = $access_restricted ? AccessResult::allowedIfHasPermission($this->account, $permission) : AccessResult::allowed();
  return AccessRestrictedLink::createLink($access_result, $cacheability, $url, $this
    ->getLinkRelationType(), $attributes);
}