class RouteEnhancer in JSON:API 8
Same name and namespace in other branches
- 8.2 src/Routing/RouteEnhancer.php \Drupal\jsonapi\Routing\RouteEnhancer
Ensures the loaded entity matches the requested resource type.
@internal
Hierarchy
- class \Drupal\jsonapi\Routing\RouteEnhancer implements RouteEnhancerInterface
Expanded class hierarchy of RouteEnhancer
1 string reference to 'RouteEnhancer'
1 service uses RouteEnhancer
File
- src/
Routing/ RouteEnhancer.php, line 15
Namespace
Drupal\jsonapi\RoutingView source
class RouteEnhancer implements RouteEnhancerInterface {
/**
* {@inheritdoc}
*/
public function applies(Route $route) {
return (bool) Routes::getResourceTypeNameFromParameters($route
->getDefaults());
}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request) {
$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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RouteEnhancer:: |
public | function |
Declares if the route enhancer applies to the given route. Overrides RouteEnhancerInterface:: |
|
RouteEnhancer:: |
public | function | Update the defaults based on its own data and the request. |