- 🇬🇧United Kingdom Eli-T Manchester
Looks like #3270141-5: Allow other modules to extend MenuItemsResource → isn't quite enough to successfully subclass MenuItemsResource in 1.2.5.
In our case, this broke because in 1.2.5, MenuItemsResource now implements ContainerInjectionInterface.
When \Drupal\jsonapi_resources\Unstable\Controller\JsonapiResourceController::processRequest() is handling a request for the subclassed resource, it calls $this->getJsonapiResource(), which ultimately uses \Drupal\Core\DependencyInjection\ClassResolver::getInstanceFromDefinition() to get the class.
This part of that function now has different behaviour
if (is_subclass_of($definition, 'Drupal\Core\DependencyInjection\ContainerInjectionInterface')) { $instance = $definition::create($this->container); } else { $instance = new $definition(); }
because in 1.2.5 we pass the if condition, unlike 1.2.4.
And because \Drupal\jsonapi_menu_items\Resource\MenuItemsResource::create() returns new self(), even when $definition is set to our subclass, the parent class is returned.
Therefore jsonapi_resources acts as if the parent class was requested rather than the subclass.
Here is an addition to the patch in #3270141-5: Allow other modules to extend MenuItemsResource → that changes self to static. I presume this will annoy PHPStan. However, this makes inheritance possible here and works with our use case. I'd be grateful to hear of any better approaches though!
- 🇬🇧United Kingdom Eli-T Manchester
On a tangential note, whilst diffing 1.2.4 and 1.2.5 to try to figure out what had changed, it felt more like this could have been a minor release than a patch release!
- Status changed to Closed: won't fix
11 months ago 7:10am 21 May 2024 - 🇬🇧United Kingdom Eli-T Manchester
Closing this issue now as we have refactored our custom module to decorate MenuItemsResource rather than extend it. This works with no changes to this module. Note you need to implement getRouteResourceTypes() on the decorator class, and in response call it on the decorated class if you do this.