In Gatsby Preview, nested paragraphs load current revision instead of latest revision

Created on 24 October 2024, about 2 months ago

Problem/Motivation

On our site we have a paragraph type that has two paragraph fields. It looks similar to the following:

  • Basic page
    • Key impacts (paragraph/entity reference revisions field):
      • Key impact paragraph type
        • Key images (paragraph/entity reference revisions field)
          • Key image paragraph type
            • Image (media reference field)
        • Key statistics (paragraph/entity reference revisions field)
          • Key statistic paragraph type
            • Statistic (plain text field)
            • Statistic description (plain long text field)

Granted, this is a LOT of nesting, I know.

The issue is that when you have a published version of a node, and then you make a change to one of these nested paragraphs, and click the Open Gatsby Preview button, the changes to the nested paragraphs aren't reflected in the preview.

Steps to reproduce

Here are the relevant enabled modules:

  • Content moderation
  • Paragraphs
  • Gatsby

Steps to reproduce:

  • Create a content type (Basic page).
  • Create a paragraph type (Key image).
  • Create a paragraph type (Key impact) which references the Key image paragraph type.
  • Add an entity reference revisions/paragraph field to the Basic page content type. It should reference the Key impact paragraph type.
  • Create a basic page. Make sure to add values to all fields, including Key impact and Key image.
  • Publish the page.
  • Edit the page. Change the value of the Key image field.
  • Click Open Gatsby Preview.

When you click Open Gatsby Preview, the value of the Key image field will be the published version, not the updated version.

Proposed resolution

After a lot of digging, I found that the solution is simple.

In src/GatsbyEntityLogger.php, the buildRelationshipJson method recursively traverses all related entities to build a JSON object containing the data needed to build the preview.

The buildRelationshipJson method has a parameter called $type, which determines if the webhook it's working on is for a build or a preview. By default, it's set to build.

In all other calls to the buildRelationshipJson method, the $type value is passed along with the other arguments. But, in the call where the method recursively calls itself, the $type value is not passed, which means it defaults to build. This means that in the following statement, the related entity is loaded via its uuid, which gets the current revision, rather than via its latest revision:

  if ($type == 'preview' && !empty($related_data['meta']['target_revision_id'])) {
    $related_entity = $this->entityTypeManager->getStorage($entity_ref_type)
      ->loadRevision($related_data['meta']['target_revision_id']);
  }
  else {
    $related_entity = $this->entityRepository->loadEntityByUuid($entity_ref_type, $related_data['id']);
  }

To resolve this, simply change

  if (!empty($related_json['data']['relationships'])) {
    $this->buildRelationshipJson($related_json['data']['relationships'], $entity_data, $included_types, $entity_ref_type, $entity_ref_bundle);
  }

to

  if (!empty($related_json['data']['relationships'])) {
  $this->buildRelationshipJson($related_json['data']['relationships'], $entity_data, $included_types, $entity_ref_type, $entity_ref_bundle<strong>, $type</strong>);
  }
πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jsutta United States

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024