You are here

protected function DataProviderEntity::isValidEntity in RESTful 7.2

Determine if an entity is valid, and accessible.

Parameters

string $op: The operation to perform on the entity (view, update, delete).

int $entity_id: The entity ID.

Return value

bool TRUE if entity is valid, and user can access it.

Throws

UnprocessableEntityException

InaccessibleRecordException

3 calls to DataProviderEntity::isValidEntity()
DataProviderEntity::remove in src/Plugin/resource/DataProvider/DataProviderEntity.php
Delete operation.
DataProviderEntity::update in src/Plugin/resource/DataProvider/DataProviderEntity.php
Update operation.
DataProviderEntity::view in src/Plugin/resource/DataProvider/DataProviderEntity.php
Read operation.

File

src/Plugin/resource/DataProvider/DataProviderEntity.php, line 902
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderEntity.

Class

DataProviderEntity
Class DataProviderEntity.

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

protected function isValidEntity($op, $entity_id) {
  $entity_type = $this->entityType;
  if (!ctype_digit((string) $entity_id) || !($entity = entity_load_single($entity_type, $entity_id))) {

    // We need to check if the entity ID is numeric since if this is a uuid
    // that starts by the number 4, and there is an entity with ID 4 that
    // entity will be loaded incorrectly.
    throw new UnprocessableEntityException(sprintf('The entity ID %s does not exist.', $entity_id));
  }
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  if (!empty($this->bundles) && !in_array($bundle, $this->bundles)) {
    return FALSE;
  }
  if ($this
    ->checkEntityAccess($op, $entity_type, $entity) === FALSE) {
    if ($op == 'view' && !$this
      ->getResourcePath()) {

      // Just return FALSE, without an exception, for example when a list of
      // entities is requested, and we don't want to fail all the list because
      // of a single item without access.
      // Add the inaccessible item to the metadata to fix the record count in
      // the formatter.
      $inaccessible_records = $this
        ->getMetadata()
        ->get('inaccessible_records');
      $inaccessible_records[] = array(
        'resource' => $this->pluginId,
        'id' => $entity_id,
      );
      $this
        ->getMetadata()
        ->set('inaccessible_records', $inaccessible_records);
      return FALSE;
    }

    // Entity was explicitly requested so we need to throw an exception.
    throw new InaccessibleRecordException(sprintf('You do not have access to entity ID %s.', $entity_id));
  }
  return TRUE;
}