You are here

public function EntityResource::get in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Plugin/rest/resource/EntityResource.php \Drupal\rest\Plugin\rest\resource\EntityResource::get()

Responds to entity GET requests.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

Return value

\Drupal\rest\ResourceResponse The response containing the entity with its accessible fields.

Throws

\Symfony\Component\HttpKernel\Exception\HttpException

File

core/modules/rest/src/Plugin/rest/resource/EntityResource.php, line 47
Contains \Drupal\rest\Plugin\rest\resource\EntityResource.

Class

EntityResource
Represents entities as resources.

Namespace

Drupal\rest\Plugin\rest\resource

Code

public function get(EntityInterface $entity) {
  if (!$entity
    ->access('view')) {
    throw new AccessDeniedHttpException();
  }
  foreach ($entity as $field_name => $field) {
    if (!$field
      ->access('view')) {
      unset($entity->{$field_name});
    }
  }
  $response = new ResourceResponse($entity, 200);

  // Make the response use the entity's cacheability metadata.
  // @todo include access cacheability metadata, for the access checks above.
  $response
    ->addCacheableDependency($entity);
  return $response;
}