Skip to content

Commit dcce0ff

Browse files
fix(next): make EntityResource constructor compatible with Drupal 10 and 11
1 parent dfa340d commit dcce0ff

File tree

3 files changed

+14
-39
lines changed

3 files changed

+14
-39
lines changed

modules/next/modules/next_jsonapi/next_jsonapi.services.yml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,6 @@ parameters:
44
services:
55
jsonapi.entity_resource:
66
class: Drupal\next_jsonapi\Controller\EntityResource
7+
autowire: true
78
arguments:
8-
- '@entity_type.manager'
9-
- '@entity_field.manager'
10-
- '@jsonapi.resource_type.repository'
11-
- '@renderer'
12-
- '@entity.repository'
13-
- '@jsonapi.include_resolver'
14-
- '@jsonapi.entity_access_checker'
15-
- '@jsonapi.field_resolver'
16-
- '@jsonapi.serializer'
17-
- '@datetime.time'
18-
- '@current_user'
199
- '%next_jsonapi.size_max%'

modules/next/modules/next_jsonapi/src/Controller/EntityResource.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,23 @@ class EntityResource extends JsonApiEntityResource {
2828
*
2929
* @var int
3030
*/
31-
protected $maxSize;
31+
protected int $maxSize;
3232

3333
/**
3434
* EntityResource constructor.
3535
*
36-
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
37-
* The entity type manager.
38-
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager
39-
* The entity type field manager.
40-
* @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
41-
* The JSON:API resource type repository.
42-
* @param \Drupal\Core\Render\RendererInterface $renderer
43-
* The renderer.
44-
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
45-
* The entity repository.
46-
* @param \Drupal\jsonapi\IncludeResolver $include_resolver
47-
* The include resolver.
48-
* @param \Drupal\jsonapi\Access\EntityAccessChecker $entity_access_checker
49-
* The JSON:API entity access checker.
50-
* @param \Drupal\jsonapi\Context\FieldResolver $field_resolver
51-
* The JSON:API field resolver.
52-
* @param \Symfony\Component\Serializer\SerializerInterface|\Symfony\Component\Serializer\Normalizer\DenormalizerInterface $serializer
53-
* The JSON:API serializer.
54-
* @param \Drupal\Component\Datetime\TimeInterface $time
55-
* The time service.
56-
* @param \Drupal\Core\Session\AccountInterface $user
57-
* The current user account.
58-
* @param int $max_size
59-
* The offset max size.
36+
* @param mixed ...$args
37+
* All constructor arguments.
6038
*/
61-
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $field_manager, ResourceTypeRepositoryInterface $resource_type_repository, RendererInterface $renderer, EntityRepositoryInterface $entity_repository, IncludeResolver $include_resolver, EntityAccessChecker $entity_access_checker, FieldResolver $field_resolver, SerializerInterface $serializer, TimeInterface $time, AccountInterface $user, int $max_size) {
62-
parent::__construct($entity_type_manager, $field_manager, $resource_type_repository, $renderer, $entity_repository, $include_resolver, $entity_access_checker, $field_resolver, $serializer, $time, $user);
63-
$this->maxSize = $max_size;
39+
public function __construct(...$args) {
40+
// Pop the last argument as $maxSize.
41+
$this->maxSize = array_pop($args);
42+
43+
// Forward the remaining arguments to the parent constructor.
44+
// We handle it this way because the parent constructor arguments
45+
// differ between Drupal 10 and Drupal 11, so using ...$args
46+
// ensures compatibility across versions.
47+
parent::__construct(...$args);
6448
}
6549

6650
/**

modules/next/modules/next_jsonapi/tests/src/Kernel/Controller/EntityResourceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function register(ContainerBuilder $container) {
7272
if ($container->hasDefinition('jsonapi.entity_resource')) {
7373
$definition = $container->getDefinition('jsonapi.entity_resource');
7474
$definition->setClass('Drupal\next_jsonapi\Controller\EntityResource')
75+
->setAutowired(TRUE)
7576
->addArgument('%next_jsonapi.size_max%');
7677
}
7778
}

0 commit comments

Comments
 (0)