Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
While using restws module to provide a json output of users I got somer notices:
Notice: Trying to get property of non-object in flag_properties_get_flagged_entities() (line 2595
Notice: Trying to get property of non-object in flag_properties_get_user_sid() (line 2640
Notice: Trying to get property of non-object in flag_properties_get_flagged_entities() (line 2595
As they can be avoided if we check the variable values before using it, I believe this path may be useful for other people.
Comment | File | Size | Author |
---|---|---|---|
#5 | fix_notices_2687243_5.patch | 3.16 KB | rodrigogc |
#2 | fix_notices_2687243_1.patch | 1.5 KB | luizsgpetri |
|
Comments
Comment #2
luizsgpetri CreditAttribution: luizsgpetri at CI&T for Pfizer, Inc. commentedComment #3
luizsgpetri CreditAttribution: luizsgpetri at CI&T for Pfizer, Inc. commentedComment #4
joachim CreditAttribution: joachim commentedAFAIK, entity metadata getters should always get the entity as the first parameter. Eg:
Comment #5
rodrigogc CreditAttribution: rodrigogc commentedTo fix these notices I create another patch that check the variable, because a Getter Callback always have a validation like on 'entity_property_verbatim_get'.
Comment #6
joachim CreditAttribution: joachim commented$entity should always be the entity. Please explain why this might be otherwise.
Comment #7
caminadaf CreditAttribution: caminadaf at CI&T for Pfizer, Inc. commentedJoachim, this is done by default even inside entity_property_verbatim_get. It is a good codification practice and you shouldn't trust that entity_metadata_wrapper will always give you an object there.
In the example of this issue, the error was reproduced by using RESTWS to generate a users.json file, since it tries to load the properties of all users (including the "anonymous user" -> user 0).
If that's too much trouble, it's simple, just load user 0 with entity_metadata_wrapper and try to access its properties. You will see that for any of the core properties it won't return a notice, since they check if the property exist using empty() or isset().
For reference, please check the following core callbacks:
entity_metadata_user_get_properties
entity_metadata_node_get_properties
entity_property_verbatim_get
entity_property_verbatim_date_get
Comment #8
joachim CreditAttribution: joachim commentedWorks fine for me:
Please provide code that causes a problem!
Comment #9
caminadaf CreditAttribution: caminadaf at CI&T for Pfizer, Inc. commentedReturns a notice on watchdog
Notice: Trying to get property of non-object in flag_properties_get_user_sid() (line 2683 of /home/fcaminada/Pfizer/Github/pfdigitalpf/source/deploy/modules/contrib/flag/flag.module).
Just like this issue's description.
Comment #10
caminadaf CreditAttribution: caminadaf at CI&T for Pfizer, Inc. commentedComment #11
joachim CreditAttribution: joachim commentedSo we have this:
That's an inconsistent behaviour in Entity module, so the bug is there, not here.
Also, these callbacks clearly are meant to have an entity parameter and not a FALSE, which is what I get in the case when I pass in a user ID of 0. So again, this looks like a bug.
It would help if these callbacks were properly documented -- I made a start here: #2310981: callback documentation for entity metadata wrappers. Without documentation to say that $entity might not be an object, I'm going to say it's a bug in Entity module.