Skip to content

Commit cabf7c5

Browse files
fix(next): make EntityResource constructor compatible with Drupal 10/11 (#882)
1 parent dfa340d commit cabf7c5

File tree

3 files changed

+14
-50
lines changed

3 files changed

+14
-50
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 & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,10 @@
22

33
namespace Drupal\next_jsonapi\Controller;
44

5-
use Drupal\Component\Datetime\TimeInterface;
6-
use Drupal\Core\Entity\EntityFieldManagerInterface;
7-
use Drupal\Core\Entity\EntityRepositoryInterface;
8-
use Drupal\Core\Entity\EntityTypeManagerInterface;
9-
use Drupal\Core\Render\RendererInterface;
10-
use Drupal\Core\Session\AccountInterface;
11-
use Drupal\jsonapi\Access\EntityAccessChecker;
12-
use Drupal\jsonapi\Context\FieldResolver;
135
use Drupal\jsonapi\Controller\EntityResource as JsonApiEntityResource;
14-
use Drupal\jsonapi\IncludeResolver;
156
use Drupal\jsonapi\Query\OffsetPage;
167
use Drupal\jsonapi\ResourceType\ResourceType;
17-
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
188
use Symfony\Component\HttpFoundation\Request;
19-
use Symfony\Component\Serializer\SerializerInterface;
209

2110
/**
2211
* Process all entity requests.
@@ -28,39 +17,23 @@ class EntityResource extends JsonApiEntityResource {
2817
*
2918
* @var int
3019
*/
31-
protected $maxSize;
20+
protected int $maxSize;
3221

3322
/**
3423
* EntityResource constructor.
3524
*
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.
25+
* @param mixed ...$args
26+
* All constructor arguments.
6027
*/
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;
28+
public function __construct(...$args) {
29+
// Pop the last argument as $maxSize.
30+
$this->maxSize = array_pop($args);
31+
32+
// Forward the remaining arguments to the parent constructor.
33+
// We handle it this way because the parent constructor arguments
34+
// differ between Drupal 10 and Drupal 11, so using ...$args
35+
// ensures compatibility across versions.
36+
parent::__construct(...$args);
6437
}
6538

6639
/**

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)