- Issue created by @Vivek Panicker
- Merge request !7626Issue #3442267: Loaded entity reference revision if it exists. → (Open) created by Vivek Panicker
- Issue was unassigned.
- Status changed to Needs review
8 months ago 1:52am 21 April 2024 - Status changed to Needs work
8 months ago 1:25pm 22 April 2024 - 🇺🇸United States smustgrave
Thank you for reporting. Appears to have test failures and will need test coverage
- 🇮🇳India Vivek Panicker Kolkata
@smustgrave can you please explain a bit more on what all is necessary now?
I can then give it a try. - 🇨🇦Canada dale42 Vancouver, Canada
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.
- 🇨🇦Canada dale42 Vancouver, Canada
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 used, 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
- 🇨🇦Canada dale42 Vancouver, Canada
Sorry, did not mean to remove the "Needs tests" tag. Re-adding.