Followup for #2002138: Use an adapter for supporting typed data on ContentEntities

- The implementation Entity::getTypedData() is a bit cheating :

$class = \Drupal::typedDataManager()->getDefinition('entity')['class'];
$this->typedData = $class::createFromEntity($this);

"I pretend I don't know what is the $class for the 'entity' data type, but I assume it has a createFromEntity() static method, which is not part of any interface and only exists on TypedData\EntityAdapter.
Patch removes EntityAdapter::createFromEntity() and makes Entity::getTypedDataAdapter() use the regular TDM::create() method for instanciating the EntityAdapter

TOO LATE - EntityInterface::getTypedData() method name is not very clear on what kind of object you actually get. We settled on naming the class and concept "entity adapter", and the code typically does $entity_adapter = $entity->getTypedData(), so naming the method getTypedDataAdapter() would be more specific/accurate.
Patch does that, and renames the corresponding property in the Entity class as well.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yched’s picture

yched’s picture

yched’s picture

Status: Active » Needs review

Doh.

fago’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Entity/Entity.php
@@ -572,19 +573,23 @@ public function toArray() {
+      $this->typedDataAdapter = $class::createInstance($definition);

It should now use TDM::create() as usual to instantiate the object.

yched’s picture

Status: Needs work » Needs review
FileSize
10.53 KB
843 bytes

Much cleaner indeed.

The last submitted patch, 1: 2346643-typed_data_adapter-followup-1.patch, failed testing.

yched’s picture

Issue summary: View changes

Status: Needs review » Needs work

The last submitted patch, 5: 2346643-typed_data_adapter-followup-5.patch, failed testing.

yched’s picture

Status: Needs work » Needs review
FileSize
11.35 KB
837 bytes

Crap, PhpStorm missed one rename.

fago’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, patch is good now assuming it comes back green.

yched’s picture

OK, so, too late for beta :-(, and I have no critical argument for renaming the method post-beta, so dropping the rename part, just keeping the "refactor instanciation part"

Keeping @fago's "RTBC if green" since this is a subset of the previous patch.

The last submitted patch, 9: 2346643-typed_data_adapter-followup-9.patch, failed testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 11: 2346643-typed_data_adapter-followup-11.patch, failed testing.

yched’s picture

So, this breaks because :
- the actual 'data type' used to determine the TypedData class is not hardcoded to 'entity' anymore, TDM::create() uses the 'data type' determined by the DataDefinition it receives - in our case, EntityDataDefinition::getDataType(),
- that method always tries to be as specific as possible given the definition it holds : in that case: 'entity:[entity_type]:[bundle]'.
- that 'entity:[entity_type]:[bundle]' data type only exists if the bundle actually exists, since EntityDeriver only creates the derivatives for official bundles

This then breaks if you create an entity with a "fake" / non-existing bundles.
There seems to be quite a few places that do that atm : in tests, but also in NodeTypeForm for example:

// Create a node with a fake bundle using the type's UUID so that we can
// get the default values for workflow settings.
// @todo Make it possible to get default values without an entity.
//   https://www.drupal.org/node/2318187
$node = $this->entityManager->getStorage('node')->create(array('type' => $type->uuid()));

Any thoughts ? :-)

yched’s picture

Issue summary: View changes
yched’s picture

Status: Needs work » Needs review
FileSize
2.95 KB
1.36 KB

Agreed on a fix with @fago : in the (Entity)DataDefinition we pass to TDM::create(), do not specify the bundle if it doesn't officially exist, so that we don't try to instanciate a non-existing data type.

Status: Needs review » Needs work

The last submitted patch, 16: 2346643-typed_data_adapter-followup-17.patch, failed testing.

The last submitted patch, 16: 2346643-typed_data_adapter-followup-17.patch, failed testing.

fgm’s picture

Same error locally: installation with this patch does not complete, failing on property user_picture being unknown, which leaves the site incompletely installed, with user 1 still being at the "placeholde-for-uid-1" stage.

yched’s picture

Status: Needs work » Needs review
FileSize
3.32 KB
1.46 KB

Headdesk.

Status: Needs review » Needs work

The last submitted patch, 21: 2346643-typed_data_adapter-followup-21.patch, failed testing.

yched’s picture

in the (Entity)DataDefinition we pass to TDM::create(), do not specify the bundle if it doesn't officially exist, so that we don't try to instanciate a non-existing data type

The issue is then that in this case, EntityDataDefinition::getPropertyDefinitions() only return BFDs, and not the per-bundle/configurable fields, since it doesn't know its own bundle...

Ideally, the "field definitions" used by EntityDataDefinition would be injected from the Entity rather than pulled from the EntityManager. Independently of this issue here, this would be a good move towards a more injectable Entity / Field API, which is also something we discussed this week in Amsterdam.

Attached patch adds a setPropertyDefinitions() to EntityDataDefinition, and uses it in Entity::getTypedData() to inject the entity's field definitions into the EntityDataDefinition / the EntityAdapter. No docs yet, just see what breaks.

yched’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 23: 2346643-typed_data_adapter-followup-23.patch, failed testing.

yched’s picture

Status: Needs work » Needs review
FileSize
6.19 KB
2.6 KB

Fixes EntityAdapterUnitTest, which still used the old createFromEntity() method.

Migrate fails beat me for now :-(

Status: Needs review » Needs work

The last submitted patch, 26: 2346643-typed_data_adapter-followup-26.patch, failed testing.

yched’s picture

Also, if anyone has a hint on how to interpret this from the testbot log :

Drupal\Tests\Core\Entity\BaseFieldDefinitionTest              16 passes
PHP Fatal error:  Call to a member function validate() on a non-object in /var/lib/drupaltestbot/sites/default/files/checkout/core/lib/Drupal/Core/Entity/ContentEntityBase.php on line 257
PHP Warning:  Invalid argument supplied for foreach() in /var/lib/drupaltestbot/sites/default/files/checkout/core/scripts/run-tests.sh on line 590

Warning: Invalid argument supplied for foreach() in /var/lib/drupaltestbot/sites/default/files/checkout/core/scripts/run-tests.sh on line 590
Drupal\Tests\Core\Entity\Controller\EntityViewControllerTest   1 passes

No clue which test triggers that "Call to a member function validate() on a non-object" error.
The tests listed before and after (BaseFieldDefinitionTest, EntityViewControllerTest) are both green...

swentel’s picture

@yched it goes wrong in Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest

swentel’s picture

Status: Needs work » Needs review
FileSize
1.88 KB
8.07 KB

This should fix that test - however, clueless about the migrate test as well though

yched’s picture

@swentel : Thanks ! Running all unitests locally would indeed have revealed which test was failing.

Removing some unneeded code in ContentEntityBaseUnitTest::setUp(), then.

The last submitted patch, 30: 2346643-typed_data_adapter-followup-30.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 31: 2346643-typed_data_adapter-followup-31.patch, failed testing.

plach’s picture

The last submitted patch, 31: 2346643-typed_data_adapter-followup-31.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.