src/Routing/Routes.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Routing/Routes.php b/src/Routing/Routes.php index f38af6e..5286512 100644 --- a/src/Routing/Routes.php +++ b/src/Routing/Routes.php @@ -217,6 +217,15 @@ class Routes implements ContainerInjectionInterface { return $routes; } + // 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); + if (!$has_file_field) { + return $routes; + } + if ($resource_type->isMutable()) { $path = $resource_type->getPath(); $entity_type_id = $resource_type->getEntityTypeId(); @@ -434,6 +443,22 @@ class Routes implements ContainerInjectionInterface { } /** + * Determines if an array of resource types lists non-internal "file" ones. + * + * @param \Drupal\jsonapi\ResourceType\ResourceType[] $resource_types + * The resource types to check. + * + * @return bool + * TRUE if there is at least one non-internal "file" resource type in the + * 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); + } + + /** * Gets the resource type from a route or request's parameters. * * @param array $parameters