RouteEnhancer.php in Drupal 9
File
core/modules/jsonapi/src/Routing/RouteEnhancer.php
View source
<?php
namespace Drupal\jsonapi\Routing;
use Drupal\Core\Routing\EnhancerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class RouteEnhancer implements EnhancerInterface {
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) {
throw new NotFoundHttpException(sprintf('The loaded entity bundle (%s) does not match the configured resource (%s).', $retrieved_bundle, $configured_bundle));
}
return $defaults;
}
}
Classes
Name |
Description |
RouteEnhancer |
Ensures the loaded entity matches the requested resource type. |