Inconsistent rendering response when unpublished content is present in an entity reference field

Created on 16 February 2024, 4 months ago
Updated 27 March 2024, 3 months ago

Problem/Motivation

When using the plugin entity_reference_export, if some of the entities referenced in a Paragraph - and possibly other entity reference revision entities are not published, the export is an object rather than an array.

Steps to reproduce

  • Install the paragraphs and rest_views_revisions modules
  • Add a paragraph type with a field_reference to a content e.g. nodes or taxonomy terms
  • Configure a display mode for that paragraph type to render the field via "Export rendered entity" formatter
  • Add a paragraph field_reference to a node bundle, referencing the paragraph type created in the previous step
  • Create a rest export view where the paragraph field is rendered via "Export rendered entity", using the display mode configured in the previous step
  • Create a node, with a paragraph which references some entities that aren't published - e.g. using an admin user
  • GET that view response. Please use an user that is not allowed to view unpublished content.

Expected result:

...
    "field_paragraphs_export": [
      {
        "id": 2,
        "bundle": "tag",
        "tags": [
          {
            "id": 1,
            "bundle": "tags",
            "name": "tag 1"
          },
          {
            "id": 2,
            "bundle": "tags",
            "name": "tag 2"
          },
          {
            "id": 4,
            "bundle": "tags",
            "name": "tag 4"
          }
        ]
      }
    ],

Actual result:

...
    "field_paragraphs_export": [
      {
        "id": 2,
        "bundle": "tag",
        "tags": {
          "0": {
            "id": 1,
            "bundle": "tags",
            "name": "tag 1"
          },
          "1": {
            "id": 2,
            "bundle": "tags",
            "name": "tag 2"
          },
          "3": {
            "id": 4,
            "bundle": "tags",
            "name": "tag 4"
          }
        }
      }
    ]

In this example, the 2nd element of the array - key "1" - is not published and therefore skipped in the rendering.

Proposed resolution

In EntityReferenceExportFormatter.php filter the $items not published and reindex the arrays should work.

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇮🇹Italy Giuseppe87

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024