diff --git a/core/modules/jsonapi/src/Routing/Routes.php b/core/modules/jsonapi/src/Routing/Routes.php index e592f007..bc355f7c 100644 --- a/core/modules/jsonapi/src/Routing/Routes.php +++ b/core/modules/jsonapi/src/Routing/Routes.php @@ -12,6 +12,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use TypeError; +use Drupal; /** * Defines dynamic routes. @@ -215,9 +217,21 @@ protected static function getFileUploadRoutesForResourceType(ResourceType $resou // File upload routes are only necessary for resource types that have file // fields. - $has_file_field = array_reduce($resource_type->getRelatableResourceTypes(), function ($carry, array $target_resource_types) { - return $carry || static::hasNonInternalFileTargetResourceTypes($target_resource_types); - }, FALSE); + try { + $has_file_field = FALSE; + $has_file_field = array_reduce($resource_type->getRelatableResourceTypes(), function ($carry, array $target_resource_types) { + return $carry || static::hasNonInternalFileTargetResourceTypes($target_resource_types); + }, FALSE); + } + catch (TypeError $e) { + Drupal::messenger()->addMessage(t( + 'JSONAPI: Fatal error looking internal file target resource types on entity %entity, carry %carry and path %path', + [ + '%entity' => $resource_type->getEntityTypeId(), + '%carry' => $resource_type->getBundle(), + '%path' => $resource_type->getPath(), + ]), 'error'); + } if (!$has_file_field) { return $routes; } @@ -336,14 +350,27 @@ protected static function getIndividualRoutesForResourceType(ResourceType $resou // Only create routes for related routes that target at least one // non-internal resource type. - if (static::hasNonInternalTargetResourceTypes($target_resource_types)) { - // Get an individual resource's related resources. - $related_route = new Route("/{$path}/{entity}/{$relationship_field_name}"); - $related_route->setMethods(['GET']); - $related_route->addDefaults([RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_SERVICE_NAME . ':getRelated']); - $related_route->addDefaults(['related' => $relationship_field_name]); - $related_route->setRequirement(RelationshipFieldAccess::ROUTE_REQUIREMENT_KEY, $relationship_field_name); - $routes->add(static::getRouteName($resource_type, "$relationship_field_name.related"), $related_route); + try { + if (static::hasNonInternalTargetResourceTypes($target_resource_types)) { + // Get an individual resource's related resources. + $related_route = new Route("/{$path}/{entity}/{$relationship_field_name}"); + $related_route->setMethods(['GET']); + $related_route->addDefaults([RouteObjectInterface::CONTROLLER_NAME => static::CONTROLLER_SERVICE_NAME . ':getRelated']); + $related_route->addDefaults(['related' => $relationship_field_name]); + $related_route->setRequirement(RelationshipFieldAccess::ROUTE_REQUIREMENT_KEY, $relationship_field_name); + $routes->add(static::getRouteName($resource_type, "$relationship_field_name.related"), $related_route); + } + } + catch (TypeError $exception) { + Drupal::messenger()->addMessage(t( + 'JSONAPI: Fatal error looking internal resource types on entity %entity, bundle %bundle, field %relationship_field_name and path %path', + [ + '%entity' => $entity_type_id, + '%bundle' => $resource_type->getBundle(), + '%relationship_field_name' => $relationship_field_name, + '%path' => $path, + ]), 'error'); + continue; } } @@ -431,9 +458,14 @@ protected static function getFileUploadRouteName(ResourceType $resource_type, $r * array; FALSE otherwise. */ protected static function hasNonInternalTargetResourceTypes(array $resource_types) { - return array_reduce($resource_types, function ($carry, ResourceType $target) { - return $carry || !$target->isInternal(); - }, FALSE); + try { + return array_reduce($resource_types, function ($carry, ResourceType $target) { + return $carry || !$target->isInternal(); + }, FALSE); + } + catch (Exception $e) { + throw $e; + } } /** @@ -447,9 +479,14 @@ protected static function hasNonInternalTargetResourceTypes(array $resource_type * given array; FALSE otherwise. */ protected static function hasNonInternalFileTargetResourceTypes(array $resource_types) { - return array_reduce($resource_types, function ($carry, ResourceType $target) { - return $carry || (!$target->isInternal() && $target->getEntityTypeId() === 'file'); - }, FALSE); + try { + return array_reduce($resource_types, function ($carry, ResourceType $target) { + return $carry || (!$target->isInternal() && $target->getEntityTypeId() === 'file'); + }, FALSE); + } + catch (Exception $e) { + throw $e; + } } /**