IEF should edit latest (not default) revision (like node edit form does)

Created on 6 May 2021, over 3 years ago
Updated 23 January 2023, almost 2 years ago

Problem/Motivation

Hello, I'm somewhat new to Drupal and Inline Entity Form, and I've run into an issue:

  • I have a piece of content that is in a moderation workflow and has a number of revisions.
  • The most recent revision is in the "draft" state and is unpublished.
  • The second-most-recent revision is in the "published" state and is published.
  • When I edit the content via /node/{id}/edit I see the "draft" revision.
  • When I edit the content via inline entity form (when it's referenced by other content) I see the "published" revision.

I'm confused why inline entity form's edit form is not behaving the same as the standard /node/{id}/edit form.
In particular this makes working with workflows a lot more difficult.

I'm not sure if I've missed some kind of configuration step that resolves this? Apologies if I have.

Steps to reproduce

  1. Create a new node.
  2. Put it in the standard content moderation workflow.
  3. Create a published revision.
  4. Create a (newer) draft revision.
  5. Edit the node via /node/{id}/edit and confirm you see the (newer) draft revision.
  6. Add the node as an entity reference on another node, and make the field use inline entity form.
  7. Edit the node via inline entity form (via the other node) and confirm you see the (older) published revision.

Proposed resolution

I have looked at how Drupal core handles loading the latest revision of a node when editing it.
Specifically in EntityRepository.php's getLatestTranslationAffectedRevision function.

From that I have amended InlineEntityFormBase.php's prepareFormState function like so:

...
      // Store the $items entities in the widget state, for further manipulation.
      foreach ($items->referencedEntities() as $delta => $entity) {

        // <New code>

        // Get latest revision of entity
        if ($entity->getEntityType()->isRevisionable()) {
          $revision = NULL;
          $storage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());

          $form_langcode = $form_state->get('langcode');
          if (!empty($form_langcode) && $entity->isTranslatable()) {
            $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $form_langcode);
            
            if ($revision_id && $entity->getLoadedRevisionId() != $revision_id) {
              $revision = $storage->loadRevision($revision_id);
            }
            
            if (!$revision || ($revision->wasDefaultRevision() && !$revision->isDefaultRevision())) {
              $revision = NULL;
            }
          }

          if (!$revision) {
            $revision_id = $storage->getLatestRevisionId($entity->id());
            if ($revision_id && $entity->getLoadedRevisionId() != $revision_id) {
              $revision = $storage->loadRevision($revision_id);
            }
          }

          if ($revision) {
            $entity = $revision;
          }
        }

        // </New code>


        // Display the entity in the correct translation.
        if ($translating) {
...

EDIT 10/5/21: Fixed a bug in the above code with translations.

I'm not sure if there are any unforeseen issues with the above, or if I'm just misunderstanding the issue.
If someone could take a look and give me your thoughts that would be appreciated.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

I searched for similar revision-related issues, but none seemed to explicitly mention this particular problem so I decided to make a new issue.
I also didn't create a patch file for this as I haven't extensively tested it - I can make a patch if needed.

Thanks!

🐛 Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024