You are here

public function ResourceTypeRepository::get in JSON:API 8.2

Same name and namespace in other branches
  1. 8 src/ResourceType/ResourceTypeRepository.php \Drupal\jsonapi\ResourceType\ResourceTypeRepository::get()

Gets a specific JSON:API resource type based on entity type ID and bundle.

Parameters

string $entity_type_id: The entity type ID.

string $bundle: The ID for the bundle to find. If the entity type does not have a bundle, then the entity type ID again.

Return value

\Drupal\jsonapi\ResourceType\ResourceType The requested JSON:API resource type, if it exists. NULL otherwise.

Overrides ResourceTypeRepositoryInterface::get

See also

\Drupal\Core\Entity\EntityInterface::bundle()

File

src/ResourceType/ResourceTypeRepository.php, line 145

Class

ResourceTypeRepository
Provides a repository of all JSON:API resource types.

Namespace

Drupal\jsonapi\ResourceType

Code

public function get($entity_type_id, $bundle) {
  assert(is_string($bundle) && !empty($bundle), 'A bundle ID is required. Bundleless entity types should pass the entity type ID again.');
  if (empty($entity_type_id)) {
    throw new PreconditionFailedHttpException('Server error. The current route is malformed.');
  }
  $cid = "jsonapi:resource_type:{$entity_type_id}:{$bundle}";
  if (!array_key_exists($cid, $this->cache)) {
    $result = NULL;
    foreach ($this
      ->all() as $resource) {
      if ($resource
        ->getEntityTypeId() == $entity_type_id && $resource
        ->getBundle() == $bundle) {
        $result = $resource;
        break;
      }
    }
    $this->cache[$cid] = $result;
  }
  return $this->cache[$cid];
}