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.
Comment | File | Size | Author |
---|---|---|---|
#17 | 2857270-entity-reference-error-17.patch | 2.78 KB | blake.thompson |
| |||
#15 | 2857270.patch | 1.75 KB | podarok |
#13 | 2857270.patch | 1.81 KB | podarok |
#4 | 2857270-interdiff.txt | 593 bytes | andy_w |
Comments
Comment #2
doublejosh CreditAttribution: doublejosh commentedIt'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.
Comment #3
doublejosh CreditAttribution: doublejosh commentedComment #4
andy_w CreditAttribution: andy_w at Numiko commentedPatch was not correctly checking for an empty field.
Comment #5
podarok#4 fixes an issue
Comment #6
doublejosh CreditAttribution: doublejosh commentedI'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.
Comment #7
andy_w CreditAttribution: andy_w at Numiko commentedCould 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.
Comment #8
andy_w CreditAttribution: andy_w at Numiko commentedIn 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?
Comment #9
bighappyface CreditAttribution: bighappyface at Rackspace for Rackspace commentedWhat is the status of this? Is #8 ready for review? I am happy to help.
Comment #10
andy_w CreditAttribution: andy_w at Numiko commentedI believe #8 is ready to review, I have been testing and seems to work as expected.
Comment #11
imadalin CreditAttribution: imadalin as a volunteer and at Numiko commentedThis 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.
Comment #12
froboyI 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.
Comment #13
podarokThere is another place where same issue exists
Attaching patch for that place too
Comment #15
podarokSorry, fixing a mistake
Comment #17
blake.thompson CreditAttribution: blake.thompson at Slalom commentedWorking 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.
Comment #18
blake.thompson CreditAttribution: blake.thompson at Slalom commentedComment #20
blake.thompson CreditAttribution: blake.thompson at Slalom commentedI went ahead and committed the latest patch.