You are here

public function MutableResourceTypeLinkProvider::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

examples/Plugin/jsonapi_hypermedia/LinkProvider/MutableResourceTypeLinkProvider.php, line 57

Class

MutableResourceTypeLinkProvider
Link provider for mutable hypermedia controls.

Namespace

Drupal\jsonapi_hypermedia\Plugin\jsonapi_hypermedia\LinkProvider

Code

public function getLink($resource_object) {
  assert($resource_object instanceof ResourceObject);
  $entity = $this
    ->loadEntityFromResourceObject($resource_object);
  $entity_operation_mapping = [
    'update' => 'update',
    'remove' => 'delete',
  ];
  $access = $entity
    ->access($entity_operation_mapping[$this->operation], NULL, TRUE);
  return AccessRestrictedLink::createLink($access, new CacheableMetadata(), $resource_object
    ->toUrl(), $this
    ->getLinkRelationType());
}