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

jasonawant created an issue. See original summary.

jasonawant’s picture

How does one suggest an edit to a change record? https://www.drupal.org/node/3036722 has the following

use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;

// Contextual information can be explicitly specified, a typical example could
// be sending out a notification email containing the node preview.
$contexts = [
  '@language.current_language_context:' . LanguageInterface::TYPE_CONTENT => new Context(new ContextDefinition('language'), 'ro'),
];
$active = $entity_repository->getActive('node', 1, $contexts);

// If the default contextual information needs to be kept, aside from the bits
// being customized, the following code should be used. 
/** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contexts_repository */
$contexts_repository = \Drupal::service('context.repository');
$contexts = $contexts_repository->getAvailableContexts();
$contexts['@language.current_language_context:' . LanguageInterface::TYPE_CONTENT] = new Context(new ContextDefinition('language'), 'ro');
$entity = $this->entityRepository->getActive('node', $value, $contexts);

where

$contexts['@language.current_language_context:' . LanguageInterface::TYPE_CONTENT] = new Context(new ContextDefinition('language'), 'ro');

should be

$language = \Drupal::service('language_manager')->getLanguage('ro');
$contexts['@language.current_language_context:' . LanguageInterface::TYPE_CONTENT] = new Context(new ContextDefinition('language'), $language);
jasonawant’s picture

Status: Active » Needs review
StatusFileSize
new4.21 KB

I'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 = [];
$storage = \Drupal::service('entity_type.manager')->getStorage('node');
$entity = $storage->load(2);
$language_manager = \Drupal::service('language_manager');
$context_definition= new ContextDefinition('language');
$entity_repository = \Drupal::service('entity.repository');
if ($entity instanceof TranslatableInterface) {
  // For each translations load the latest revision.
  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);
  }
}

$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.

Status: Needs review » Needs work

The last submitted patch, 3: 3080995-3.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jasonawant’s picture

Issue summary: View changes
Status: Needs work » Needs review
Related issues: +#3088341: Add ability to query on most recent language specific revision
StatusFileSize
new4.29 KB
new2.33 KB

I'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.

Status: Needs review » Needs work

The last submitted patch, 5: 3080995-4.patch, failed testing. View results

jasonawant’s picture

Status: Needs work » Needs review

So 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.

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

Drupal 8.7.9 was released on November 6 and is the final full bugfix release for the Drupal 8.7.x series. Drupal 8.7.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.8.0 on December 4, 2019. (Drupal 8.8.0-beta1 is available for testing.)

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.

joseph.olstad’s picture

This looks like the right approach

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
joseph.olstad’s picture

chasing 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.

jasonawant’s picture

As I recall, it was a lot of brain work to get into this problem space Best of luck Joseph!

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.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

This 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

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.

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.