When adding an entity reference field pointing to a node I am getting the following error:

Fatal error: Call to a member function getString() on null in datalayer.module on line 567

This appears to be caused by the lack of a name parameter attached to the Node object. I am assuming the intended functionality is to output the entity title - given that entityInterface specifies the label() method would it be a better idea to make use of this method (which resolves the issue).

Steps to reproduce:
1. Enable 'Include enabled field values' on the data layer settings
2. Add an entity reference field pointing to content (node) entities
3. Tick to 'Expose in dataLayer' in the field settings
4. View a page with that field populated

Expected result:
See the title of the selected content within the data layer array.

Actual result:
Fatal error: Call to a member function getString() on null in datalayer.module on line 567

I have attached a patch which seems to resolve this issue.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andywhale created an issue. See original summary.

doublejosh’s picture

Priority: Normal » Major
Status: Needs review » Needs work

It's a bit unclear what folks would want output via the reference.

I would actually push to make the reference dataLayer item an object, and include title and entityId at the outset. This will be the most flexible over time.

We should probably also confirm the behavior for other references including mostly user and taxonomy term as these three are probably most common.

doublejosh’s picture

Priority: Major » Normal
andy_w’s picture

Patch was not correctly checking for an empty field.

podarok’s picture

Priority: Normal » Critical
Status: Needs work » Reviewed & tested by the community

#4 fixes an issue

doublejosh’s picture

I'd still like to see this output as an object rather than single value. It will too quickly become a need to expose other reference entity data, so let's start the API off with room to grow and avoid breaking changes.

andy_w’s picture

Could we store the id, label & bundle (all items on the the contentEntityBase) as an array on the datalayer? I also found an issue with entity reference items with more than one value.

andy_w’s picture

In fact off of the back of doublejosh's comment, would it be beneficial to include the option to alter the value based on the entity. This may benefit from a datalayer plugin in the future?

bighappyface’s picture

Status: Reviewed & tested by the community » Needs review

What is the status of this? Is #8 ready for review? I am happy to help.

andy_w’s picture

I believe #8 is ready to review, I have been testing and seems to work as expected.

imadalin’s picture

This is just a quick small change on last @andywhale's patch.
We encountered an error on one of the entity reference fields and checking if the field's entity property is actually an EntityInterface instance seems to at least not trigger a fatal error.

Until I'd be able to find exactly why a certain field would cause the issue I'm adding the patch.

froboy’s picture

I ran into a similar error with a deleted block in a panelized page. Not sure why, but was getting this error when trying to edit the page. This patch fixed things up.

podarok’s picture

Status: Needs review » Needs work

The last submitted patch, 13: 2857270.patch, failed testing. View results

podarok’s picture

Sorry, fixing a mistake

Status: Needs review » Needs work

The last submitted patch, 15: 2857270.patch, failed testing. View results

blake.thompson’s picture

Working from podarok's patch in #15. I kept the checks around the an entity's user entity but I don't think it makes sense to add it as an array here, since we already have the entityUid and this is at the moment just author name as entityName.

I'm also not sure there needs to be an alter hook for an entity reference since they can modify the object before output, though if it's desirable to allow altering field data as it's added I think it should be done for all fields right before the return in _datalayer_field_get_value(). A module could easily load the entity with the ID on the array so it doesn't seem necessary to pass the entity itself.

I swapped out the rest of the $var->name->getString() instances for $var->label() as well.

blake.thompson’s picture

Status: Needs work » Needs review

  • blake.thompson committed e34bf9b on 8.x-1.x
    Issue #2857270 by andywhale, podarok, imadalin, blake.thompson: Entity...
blake.thompson’s picture

Status: Needs review » Fixed

I went ahead and committed the latest patch.

Status: Fixed » Closed (fixed)

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