Loads incorrect paragraph revision when collecting words

Created on 6 April 2023, over 1 year ago
Updated 21 April 2023, over 1 year ago

Problem/Motivation

This module loads the incorrect revision when loading the paragraph entity for a field item in Drupal\node_read_time\Calculate\ReadingTime::collectWords(). The "entity_reference_revisions" fields store a revision ID of the paragraph in order to support different revisions of the paragraph entity related to different revisions of the parent entity (ie node).

Code with bug:
Drupal\node_read_time\Calculate\ReadingTime::collectWords() - The following code incorrectly loads the entity with ->load($item['target_id']) instead of ->loadRevision($item['target_revision_id']).

        elseif ($field->getType() == 'entity_reference_revisions') {

          $fieldStorage = $entity_fields[$k]
            ->get('fieldStorage');
          if ($fieldStorage) {
            $entityType = $fieldStorage->get('settings')['target_type'];
            $list = $entity
              ->get($k)
              ->getValue();

            foreach ($list as $item) {
              // If we are in preview mode, paragraphs are still unknown.
              if (empty($item['target_id'])) {
                continue;
              }

              $referenceRevisionEntity = $this->entityTypeManager
                ->getStorage($entityType)
                ->load($item['target_id']); // <<<< BUG: LOADS INCORRECT REVISION

              $this->collectWords($referenceRevisionEntity);
            }
          }

        }

Steps to reproduce

  • Enable content moderation for a content type that has a paragraph field with paragraphs that have readable fields.
  • Configure that content type to have a node_read_time calculation.
  • Create a node for the test content type, enter in a lot of text for a paragraph in the paragraphs field.
  • Save a new node revision.
  • Note the read time calculated.
  • Edit the node again.
  • Double the amount of text in the paragraph.
  • Save a new node revision.
  • BUG: Read time calculation reflects the text of the first node revision.

Proposed resolution

This module should let the field type load the entity to ensure that the correct revision is loaded instead of assuming field item value keys.

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States recrit

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

Comments & Activities

Production build 0.71.5 2024