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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

luizsgpetri created an issue. See original summary.

luizsgpetri’s picture

luizsgpetri’s picture

Status: Active » Needs review
joachim’s picture

Status: Needs review » Postponed (maintainer needs more info)

AFAIK, entity metadata getters should always get the entity as the first parameter. Eg:

/**
 * Gets the property just as it is set in the data.
 */
function entity_property_verbatim_get($data, array $options, $name, $type, $info) {
rodrigogc’s picture

Version: 7.x-3.x-dev » 7.x-3.9
Priority: Normal » Major
Status: Postponed (maintainer needs more info) » Needs review
FileSize
3.16 KB

To 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'.

joachim’s picture

Version: 7.x-3.9 » 7.x-3.x-dev
Priority: Major » Normal
Status: Needs review » Postponed (maintainer needs more info)
+++ b/source/deploy/modules/contrib/flag/flag.module
@@ -2622,24 +2622,29 @@ function flag_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new)
+  if (is_object($entity) && isset($property_info['flag_name'])) {

$entity should always be the entity. Please explain why this might be otherwise.

caminadaf’s picture

Status: Postponed (maintainer needs more info) » Needs review

Joachim, 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

joachim’s picture

Status: Needs review » Postponed (maintainer needs more info)

Works fine for me:

  $anon = user_load(0);
  dsm($anon);
  $wrapper = entity_metadata_wrapper('user', $anon);
  $flaggings = $wrapper->flag_bookmarks_flagged->value();
  dsm($flaggings);
  $flagged = $wrapper->flag_user_flag->value();
  dsm($flagged);

Please provide code that causes a problem!

caminadaf’s picture

  $wrapper = entity_metadata_wrapper('user', 0);
  $flagged = $wrapper->flag_sid->value();

Returns 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.

caminadaf’s picture

Status: Postponed (maintainer needs more info) » Needs review
joachim’s picture

Status: Needs review » Closed (works as designed)

So we have this:

  // Getter callbacks get an entity:
  $anon = user_load(0);
  $wrapper = entity_metadata_wrapper('user', $anon);
  // Getter callbacks do NOT get an entity:
  $wrapper = entity_metadata_wrapper('user', 0);

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.