Problem/Motivation

Currently when invoking field methods we iterate over the entity translations and call the desired method on each field for each entity translation. However there is no specific order in which we'll iterate over the translations, which might be a problem for contrib and custom if you have a complex field, which updates it's value in the presave method and depends on the entity translation which has been currently edited.

For example if you save a referenced entity in the field presave method with a new revision but before doing so check if the referenced entity is already saved in a new revision. The referenced entity will be changed only for the currently edited entity translation, which means we have to ensure that the field presave method is called first for the current entity translation.

Proposed resolution

Change the order of the entity translation languages over which we iterate in ContentEntityStorageBase::invokeFieldMethod, so that the current entity translation will be processed as the very first one.

Remaining tasks

Review

User interface changes

none

API changes

none

Data model changes

none

Comments

hchonov created an issue. See original summary.

hchonov’s picture

StatusFileSize
new6.37 KB
hchonov’s picture

StatusFileSize
new7.46 KB
hchonov’s picture

StatusFileSize
new6.33 KB
new7.42 KB

Oups forget to rename the file name of the entity class.

The last submitted patch, 2: field_invocation_order_failing_test.patch, failed testing.

The last submitted patch, 3: 2755525-3.patch, failed testing.

The last submitted patch, 4: field_invocation_order_failing_test.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 4: 2755525-4.patch, failed testing.

hchonov’s picture

Status: Needs work » Needs review
StatusFileSize
new5.95 KB
new7.04 KB

The last submitted patch, 9: field_invocation_order_failing_test.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 9: 2755525-9.patch, failed testing.

hchonov’s picture

Status: Needs work » Needs review
webflo’s picture

+++ b/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
@@ -436,6 +436,14 @@ protected function invokeFieldMethod($method, ContentEntityInterface $entity) {
+      $key = array_search($current_entity_langcode, $langcodes);
+      unset($langcodes[$key]);

We can do it in one line. $langcodes = array_diff($langcodes, [$current_entity_langcode]);

hchonov’s picture

StatusFileSize
new7.01 KB
new726 bytes

@webflo done.

gábor hojtsy’s picture

I think this patch makes sense. I don't think it is a backwards compatibility problem to better define an order that was not defined before. Nonetheless I asked Berdir and plach to look at this, if either of them are available :)

gábor hojtsy’s picture

Status: Needs review » Reviewed & tested by the community

Anyway, no need to block this on them I think :)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 2755525-14.patch, failed testing.

gábor hojtsy’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: +sprint, +language-content

Random fail.

fail: [Browser] Line 248 of core/modules/system/src/Tests/Update/UpdatePathTestBase.php:
GET http://localhost/checkout/update.php/start?id=2&op=do_nojs returned 0 (0 bytes).

fail: [Other] Line 264 of core/modules/system/src/Tests/Update/UpdatePathTestBase.php:
Schema key block.block.bartik_account_menu:settings.cache failed with: missing schema

fail: [Other] Line 264 of core/modules/system/src/Tests/Update/UpdatePathTestBase.php:
Schema key block.block.bartik_breadcrumbs:settings.cache failed with: missing schema
...

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 2755525-14.patch, failed testing.

gábor hojtsy’s picture

Status: Needs work » Reviewed & tested by the community

Git checkout error on testbot.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 2755525-14.patch, failed testing.

hchonov’s picture

Status: Needs work » Reviewed & tested by the community

It has been a random test failure, so putting back to RTBC.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 14: 2755525-14.patch, failed testing.

gábor hojtsy’s picture

Status: Needs work » Reviewed & tested by the community

Yet another random fail.

hchonov’s picture

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

I think this one should be target against 8.2.x and probably per cherry pick taken into 8.1.x as well.

effulgentsia’s picture

Issue tags: +rc deadline
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This looks pretty solid. Looking at ContentEntityStorageBase the other similar invocation by language is ::invokeTranslationHooks() - should we make it behave similarly? I.e starting with the current entity's language? Maybe that doesn't make much sense because this is about firing the translation_insert and translation_delete hooks.

In discussion with @effulgentsia, @catch, @xjm and @cottser we decided that this patch should probably only go in 8.2.x as changing the order hooks are fired in is a behaviour change and this is a task not a bug.

Committed e0af755 and pushed to 8.2.x. Thanks!

  • alexpott committed e0af755 on 8.2.x
    Issue #2755525 by hchonov: Invoke field methods first on the current...
hchonov’s picture

@alexpott like you've mentioned the ::invokeTranslationHooks function is calling the hooks for the new or the deleted translation and this happens with the appropriate entity translation object, therefore everything there looks good. I cannot think at least at the moment of a case where the order of the entity translations for the translation insert and delete hooks does matter.

Thanks for accepting and commiting this change!

  • alexpott committed e0af755 on 8.3.x
    Issue #2755525 by hchonov: Invoke field methods first on the current...

  • alexpott committed e0af755 on 8.3.x
    Issue #2755525 by hchonov: Invoke field methods first on the current...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gábor hojtsy’s picture

Issue tags: -sprint

Removing from sprint, thanks all again!