Overview

I've been building some content templates, by hand, for Drupal CMS. We need to be able to return image shapes to SDC props, like {image_url, alt}, from a normal two-level media reference field on a node.

You would think a prop expression like this would do the trick (assuming that yo_ho is a normal media reference field):

'ℹ︎␜entity:node:foo␝yo_ho␞␟{src↝entity␜␜entity:media:image␝field_media_image␞␟entity␜␜entity:file␝uri␞␟url,alt↝entity␜␜entity:media:image␝field_media_image␞␟alt}'

But you'd be wrong, because there is a bug in FieldObjectPropsExpression::fromString(). The nature of the bug, according to Wim in Slack:

  • \Drupal\experience_builder\PropExpressions\StructuredData\ReferenceFieldPropExpression allows chaining of references: public readonly ReferenceFieldPropExpression|FieldPropExpression|FieldObjectPropsExpression $referenced
  • BUT FieldObjectPropsExpression::fromString()’s logic does not yet do that:
            $objectPropsToFieldTypeProps[$sdc_obj_prop_name] = new ReferenceFieldPropExpression(
              new FieldPropExpression($entity_data_definition, $field_name, NULL, $field_instance_prop_name),
              FieldPropExpression::fromString(self::PREFIX . $field_prop_ref_expr)
            );
    

Proposed resolution

It's a one-line fix. We need to call StructuredDataPropExpression::fromString(), not FieldPropExpression::fromString().

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

phenaproxima created an issue. See original summary.

wim leers’s picture

Title: Cannot parse a prop expression that traverses multiple levels of entity references » FieldObjectPropsExpression::fromString() fails on expression that traverses multiple levels of entity references
Component: … to be triaged » Data model

This is the original bug, but would be great to actually test it for ReferenceFieldPropExpression and ReferenceFieldTypePropExpression too!

wim leers’s picture

Issue tags: +stable target
phenaproxima’s picture

Status: Active » Needs review
wim leers’s picture

Status: Needs review » Reviewed & tested by the community
wim leers’s picture

Status: Reviewed & tested by the community » Fixed

Yay for unit tests and the absence of layers of indirection (such as these prop expressions being defined by plugin types). This is the most tightly scoped MR I've seen in a while 🕺

Status: Fixed » Closed (fixed)

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