Problem/Motivation

After going through the following issues

and then debugging, led to the discovery that EntityReferenceFormatterBase class does not load the proper revision based on the revision of the entity where it the reference field lies.
It loads the default revision of the entity which is referenced, rather than loading the revision based on the revision of the entity it is attached to.

Steps to reproduce

Follow the steps mentioned in the description of https://www.drupal.org/project/paragraphs_table/issues/3441247.

Proposed resolution

In EntityReferenceFormatterBase::prepareView() load the proper revision of the entity which is referenced.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

CommentFileSizeAuthor
#8 3442267-ent-ref-rev.patch1.59 KBdale42

Issue fork drupal-3442267

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Vivek Panicker created an issue. See original summary.

vivek panicker’s picture

Title: Revision not loaded » Entity Reference Revision not loaded in EntityReferenceFormatterBase

vivek panicker’s picture

Assigned: vivek panicker » Unassigned
Status: Active » Needs review
smustgrave’s picture

Version: 10.3.x-dev » 11.x-dev
Status: Needs review » Needs work
Issue tags: +Needs tests

Thank you for reporting. Appears to have test failures and will need test coverage

vivek panicker’s picture

@smustgrave can you please explain a bit more on what all is necessary now?
I can then give it a try.

dale42’s picture

Priority: Normal » Major
Issue tags: -Needs tests +content moderation

This issue causes the display of invalid information on the Content Moderation "Latest version" tab. Specifically, it does not actually display the data from the latest version, it displays information from the default/published version. Adding the "content moderation" issue tag.

After reading the description of priorities I believe it qualifies a Major, as it is displaying incorrect data.

dale42’s picture

StatusFileSize
new1.59 KB

target_revision_id does not appear on every item. I expect this is why the tests are failing.

I have an alternative solution, also attached as a file, that is solving the issue on my system. I don't believe this is the best solution, but I think it will advance discussion on the issue.

$item sometimes has a target_revision_id and sometimes not. This code uses the target_revision_id if available, and falls back to target_id when it isn't. According to the comments "multiple entity load" should be used, so this was used to load the revised version.

Perhaps all items should have a target_revision_id so that all the entities could be loaded by revision id? This would work even if revisions were not enabled, since every entity has a revision id. This would require an upstream change to what ever is producing $item.

There is also EntityReferenceRevisionsFormatterBase. I'm not sure where it fits in the picture.

--- core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php	2024-06-20 09:17:49
+++ core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBaseFix.php	2024-06-20 09:21:59
@@ -122,6 +122,7 @@
     // "entity reference item lists" being displayed. We thus cannot use
     // \Drupal\Core\Field\EntityReferenceFieldItemList::referencedEntities().
     $ids = [];
+    $vids = [];
     foreach ($entities_items as $items) {
       foreach ($items as $item) {
         // To avoid trying to reload non-existent entities in
@@ -130,13 +131,25 @@
         // at FALSE.
         $item->_loaded = FALSE;
         if ($this->needsEntityLoad($item)) {
-          $ids[] = $item->target_id;
+          if (!is_null($item->target_revision_id)) {
+            $vids[] = $item->target_revision_id;
+          }
+          else {
+            $ids[] = $item->target_id;
+          }
         }
       }
     }
-    if ($ids) {
+    if ($ids || $vids) {
       $target_type = $this->getFieldSetting('target_type');
-      $target_entities = \Drupal::entityTypeManager()->getStorage($target_type)->loadMultiple($ids);
+      $storage = \Drupal::entityTypeManager()->getStorage($target_type);
+      $target_entities = ($ids) ? $storage->loadMultiple($ids) : [];
+      if ($vids) {
+        $target_entities_vids = $storage->loadMultipleRevisions($vids);
+        foreach ($target_entities_vids as $target_entity) {
+          $target_entities[$target_entity->id()] = $target_entity;
+        }
+      }
     }
 
     // For each item, pre-populate the loaded entity in $item->entity, and set

dale42’s picture

Issue tags: +Needs tests

Sorry, did not mean to remove the "Needs tests" tag. Re-adding.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.