You are here

class RouteEnhancer in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Routing/RouteEnhancer.php \Drupal\jsonapi\Routing\RouteEnhancer

Ensures the loaded entity matches the requested resource type.

@internal JSON:API maintains no PHP API since its API is the HTTP API. This class may change at any time and this will break any dependencies on it.

Hierarchy

Expanded class hierarchy of RouteEnhancer

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

1 string reference to 'RouteEnhancer'
jsonapi.services.yml in core/modules/jsonapi/jsonapi.services.yml
core/modules/jsonapi/jsonapi.services.yml
1 service uses RouteEnhancer
jsonapi.route_enhancer in core/modules/jsonapi/jsonapi.services.yml
Drupal\jsonapi\Routing\RouteEnhancer

File

core/modules/jsonapi/src/Routing/RouteEnhancer.php, line 18

Namespace

Drupal\jsonapi\Routing
View source
class RouteEnhancer implements EnhancerInterface {

  /**
   * {@inheritdoc}
   */
  public function enhance(array $defaults, Request $request) {
    if (!Routes::isJsonApiRequest($defaults)) {
      return $defaults;
    }
    $resource_type = Routes::getResourceTypeNameFromParameters($defaults);
    $entity_type_id = $resource_type
      ->getEntityTypeId();
    if (!isset($defaults[$entity_type_id]) || !($entity = $defaults[$entity_type_id])) {
      return $defaults;
    }
    $retrieved_bundle = $entity
      ->bundle();
    $configured_bundle = $resource_type
      ->getBundle();
    if ($retrieved_bundle != $configured_bundle) {

      // If the bundle in the loaded entity does not match the bundle in the
      // route (which is set based on the corresponding ResourceType), then
      // throw an exception.
      throw new NotFoundHttpException(sprintf('The loaded entity bundle (%s) does not match the configured resource (%s).', $retrieved_bundle, $configured_bundle));
    }
    return $defaults;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteEnhancer::enhance public function Updates the defaults for a route definition based on the request. Overrides EnhancerInterface::enhance