Problem

When using the "Custom element: Rendered entity" (entity_ce_render) formatter with the explicit JSON format, entity reference fields that are output as
props (not slots) also use explicit format with element/props separation. This doesn't make sense when rendered as prop, you want the data and are not interested in the prop/slot separation. (This worked nice in implicit format.)

Current output:

{
    "element": "marketing-features",
    "props": {
      "features": [
        {
          "element": "feature-item",
          "props": {
            "title": "Feature One",
            "icon": "..."
          }
        }
      ]
    }
  }

Expected output:

{
    "element": "marketing-features",
    "props": {
      "features": [
        {
          "element": "feature-item",
          "title": "Feature One",
          "icon": "..."
        }
      ]
    }
  }

Additionally we might want to consider ommitting the element attribute when rendering as prop..

Steps to reproduce

  1. Configure custom_elements.settings.json_format to explicit
  2. Create an entity with an entity reference field using entity_ce_render formatter configured as a prop (not slot)
  3. View the JSON output - nested elements use explicit format with props wrapper

Proposed resolution

  1. In EntityReferenceCeFieldFormatter::build(), pass 'explicit' => FALSE in the normalizer context when outputting entity references as
    props (attributes), so nested elements use implicit/flat format.
  2. Add a formatter setting "Hide element key" (default: TRUE for single-value refs) to omit the element key from the output when it's redundant.

Remaining tasks

  • Implement the fix
  • Add/update tests

User interface changes

New checkbox in entity_ce_render formatter settings: "Hide element key" (or make it default behavior for single-value fields).

API changes

For explicit output format the ce-enttiy-reference output changes *when* configured as prop. But that's what the issue is about.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

fago created an issue. See original summary.

fago’s picture

Status: Active » Needs review

added MR, testing.

  • fago committed fecca156 on 3.x
    fix: #3571516 entity_ce_render formatter outputs nested elements with...
fago’s picture

Status: Needs review » Fixed

Works great, comes with tests, all good - merged.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.