Come together with the global Drupal community in Rotterdam, 28 Sept – 1 Oct 2026. Sessions, contribution, connection, and Early Bird savings until 8 June.
+++ b/src/Configuration/ResourceConfig.php
@@ -68,16 +60,6 @@ class ResourceConfig implements ResourceConfigInterface {
- public function setEntityTypeId($entity_type_id) {
@@ -85,13 +67,6 @@ class ResourceConfig implements ResourceConfigInterface {
- public function setTypeName($type_name) {
@@ -99,13 +74,6 @@ class ResourceConfig implements ResourceConfigInterface {
- public function setPath($path) {
@@ -113,20 +81,6 @@ class ResourceConfig implements ResourceConfigInterface {
- public function setBundleId($bundle_id) {
We remove all setters, because they make the object mutable.
+++ b/src/Configuration/ResourceConfig.php
@@ -113,20 +81,6 @@ class ResourceConfig implements ResourceConfigInterface {
- public function getStorage() {
- return $this->entityTypeManager->getStorage($this->entityTypeId);
- }
We also delete this method, because it always needs the service to be injected, i.e. it's a hard blocker to making ResourceConfig a value object.
+++ b/src/Configuration/ResourceConfig.php
@@ -134,11 +88,23 @@ class ResourceConfig implements ResourceConfigInterface {
- public function __construct(EntityTypeManagerInterface $entity_type_manager) {
- $this->entityTypeManager = $entity_type_manager;
...
+ public function __construct($entity_type_id, $bundle_id, $path, $type_name, $deserialization_target_class) {
Rather than retrieving all this "automatically", we move the responsibility for doing this to the ResourceManager service.
All this made me realize I could've gone slightly further in this patch: since the path and "JSON API type" or "type name" are parameters 3 and 4 and are calculated entirely from parameters 1 and 2 (entity type ID + bundle), I could just move that into the constructor for ResourceConfig. Doing that next.
But quite importantly, it also forces us to use 'realistic' JSON API types in our tests.
In this test for example, we were using node, but that's impossible to ever see in the real world; in the real world, Drupal 8's JSON API module will always include the bundle in the JSON API type as well, so something like node--article or node--foobarbazsomething.
In other words: this change forces us to clean things up in tests, to match the real-world.
Most notably, it forces this clean-up, where the path and type were completely independent from the entity type ID and bundle ID.
+++ b/tests/src/Unit/RequestHandlerTest.php
@@ -45,7 +45,7 @@ class RequestHandlerTest extends UnitTestCase {
- $resource_config = new ResourceConfig(NULL, NULL, NULL, NULL, NULL);
+ $resource_config = new ResourceConfig($this->randomMachineName(), $this->randomMachineName(), NULL);
And also quite importantly, it allows us to show more explicitly in the test that we can use random entity type ID and random bundle ID in those cases where we're fabricating a ResourceConfig object just to comply with requirements for other objects. In other words: the use of random machine names here shows provably that these objects are effectively unused.
Comments
Comment #2
wim leers.
Comment #3
wim leersNote that #2 is relative to #2841048: Remove ResourceConfig::getGlobalConfig(), and stop injecting the config factory service in ResourceConfig value objects, it will fail.
Comment #4
wim leersWe remove all setters, because they make the object mutable.
We also delete this method, because it always needs the service to be injected, i.e. it's a hard blocker to making
ResourceConfiga value object.Rather than retrieving all this "automatically", we move the responsibility for doing this to the
ResourceManagerservice.And as you can tell, it's actually barely makes an difference here!
If you would prefer that, we could move this logic to a
public static ResourceConfig::create()method.This was the only place that was using
ResourceConfig::getStorage()! So, it was not at all hard to remove that method.A unit test for a pure value object is nonsensical. So, we remove this.
Comment #6
wim leersThis is specifically blocking #2841056: Remove use of plugins.
Comment #7
wim leersComment #8
wim leersAlso, this is a net reduction:
7 files changed, 34 insertions, 162 deletions:)Comment #9
e0ipsoComment #10
e0ipsoComment #11
wim leersYay! The retested patch came back green, see https://www.drupal.org/pift-ci-job/566430.
Comment #12
e0ipsoWe should stop mocking this now that it's just a value object. I'm working on it.
Comment #13
e0ipsoTests will fail, but sharing my WIP for @Wim Leers to continue.
Comment #14
wim leersDone.
All this made me realize I could've gone slightly further in this patch: since the path and "JSON API type" or "type name" are parameters 3 and 4 and are calculated entirely from parameters 1 and 2 (entity type ID + bundle), I could just move that into the constructor for
ResourceConfig. Doing that next.Comment #15
wim leersNow also finished that additional step.
Comment #16
wim leersSo this is the key change.
Which enables this simplification in the actual module logic.
And simplifications like this in tests.
But quite importantly, it also forces us to use 'realistic' JSON API types in our tests.
In this test for example, we were using
node, but that's impossible to ever see in the real world; in the real world, Drupal 8's JSON API module will always include the bundle in the JSON API type as well, so something likenode--articleornode--foobarbazsomething.In other words: this change forces us to clean things up in tests, to match the real-world.
Most notably, it forces this clean-up, where the path and type were completely independent from the entity type ID and bundle ID.
And also quite importantly, it allows us to show more explicitly in the test that we can use random entity type ID and random bundle ID in those cases where we're fabricating a
ResourceConfigobject just to comply with requirements for other objects. In other words: the use of random machine names here shows provably that these objects are effectively unused.Comment #17
wim leersSo with #14 and #15, we have even a further net reduction:
compared to
7 files changed, 34 insertions, 162 deletionsin #2.
Net reduction of 172 LoC vs 128.
Comment #18
e0ipsoI think this is a fair change after accepting that we'll only support bundle based resources… ever
Merging.
Comment #20
e0ipsoComment #21
wim leers#18: exactly! And yay, this means #2841056: Remove use of plugins is now unblocked!