function EntityResource::get

Same name and namespace in other branches
  1. 9 core/modules/rest/src/Plugin/rest/resource/EntityResource.php \Drupal\rest\Plugin\rest\resource\EntityResource::get()
  2. 8.9.x core/modules/rest/src/Plugin/rest/resource/EntityResource.php \Drupal\rest\Plugin\rest\resource\EntityResource::get()
  3. 10 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.

\Symfony\Component\HttpFoundation\Request $request: The incoming request.

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 128

Class

EntityResource
Represents entities as resources.

Namespace

Drupal\rest\Plugin\rest\resource

Code

public function get(EntityInterface $entity, Request $request) {
    $response = new ResourceResponse($entity, 200);
    // @todo Either remove the line below or remove this todo in https://www.drupal.org/project/drupal/issues/2973356
    $response->addCacheableDependency($request->attributes
        ->get(AccessAwareRouterInterface::ACCESS_RESULT));
    $response->addCacheableDependency($entity);
    if ($entity instanceof FieldableEntityInterface) {
        foreach ($entity as $field_name => $field) {
            
            /** @var \Drupal\Core\Field\FieldItemListInterface $field */
            $field_access = $field->access('view', NULL, TRUE);
            $response->addCacheableDependency($field_access);
            if (!$field_access->isAllowed()) {
                $entity->set($field_name, NULL);
            }
        }
    }
    $this->addLinkHeaders($entity, $response);
    return $response;
}

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