Problem/Motivation
When using content moderation and translations, EntityRepository::getActive does not return the most recent translated revision.
To set this up, create a node with 2 pending draft translation revisions. I've been using lightning with paragraphs_demo to quickly set this up.
$nid = 1;
$latest_revisions = [];
$storage = \Drupal::service('entity_type.manager')->getStorage('node');
$entity = $storage->load($nid);
$language_manager = \Drupal::service('language_manager');
$context_definition= new ContextDefinition('language');
/* @var \Drupal\Core\Entity\EntityRepository $entity_repository */
$entity_repository = \Drupal::service('entity.repository');
if ($entity instanceof TranslatableInterface) {
foreach ($entity->getTranslationLanguages() as $language) {
$new_context = new Context($context_definition, $language_manager->getLanguage($language->getId()));
$contexts = [
'@language.current_language_context:' . LanguageInterface::TYPE_CONTENT => $new_context,
'@language.current_language_context:' . LanguageInterface::TYPE_INTERFACE => $new_context
];
$latest_revisions[$language->getId()] = $entity_repository->getActive($entity->getEntityTypeId(), $entity->id(), $contexts);
}
}
With the above,
$latest_revisions['en']->label(); returns the expected title of the pending English translation revision.
$latest_revisions['de']->label(); returns the unexpected title of the current English translation revision.
Proposed resolution
TBD
Remaining tasks
TBD
User interface changes
TBD
API changes
TBD
Data model changes
TBD
Release notes snippet
TBD
Comments
Comment #2
jasonawantHow does one suggest an edit to a change record? https://www.drupal.org/node/3036722 has the following
where
should be
Comment #3
jasonawantI've edited the change record > https://www.drupal.org/node/3036722.
Some additional notes
I did learn about this new service, which looks to support workspaces, see https://www.drupal.org/node/3036722
When using it though, it does not appear to return the most recent translated revision. For example, when using the following with a node with a pending German and English draft revisions
$latest_revisions['de']->label();returns the published English revision title.$latest_revisions['de']->getTranslation('de')->label();returns the pending draft German revision title.$latest_revisions['en']->getTranslation('de')->label();returns the German revision title from node_field_revision associated with the value from$latest_revisions['en']->getRevisionId();I would expect entity repository getActive() would return the most recent revision so that
$latest_revisions['de']->label();returns the expected German draft revision title.There does not appear to be a revision context to use with getActive.
I tried to write a test that would express this apparent bug, but either there is no bug or my test isn't written correctly.
I'm going to park this here in hopes to get some eyes on this until I can return to it.
Comment #5
jasonawantI've updated the issue summary. I've updated the test and uploaded new patch and interdiff. I've added a new related issue #3088341: Add ability to query on most recent language specific revision.
However, while the test passes locally, I still experience the bug using the scenario and code in the issue summary.
Comment #7
jasonawantSo the test failures appear to be related to EntityKernelTestBase usage of entity.manager service, which has been deprecated. Is there something I should do to remedy that?
I'm still looking for feedback to 1) validate the problem in the issue summary and 2) review tests to reproduce the problem.
Comment #10
joseph.olstadThis looks like the right approach
Comment #13
joseph.olstadchasing this right now, I just noticed that jasonawant is trying to solve the same problem for the same module as I am trying to solve.
patch #5 needs a reroll, was kind of hoping for an easier way, maybe a direct query to the db will have to be done.
Comment #14
jasonawantAs I recall, it was a lot of brain work to get into this problem space Best of luck Joseph!
Comment #17
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
The test patch in #5 will need to be updated for D10 coding standards and deprecations.
If still experiencing it hopefully we get some traction