I've got a cron job which executes an entity field query to count the number of some nodes associated with a user (for each user in the system). Whenever this runs I get the following in the watchdog logs:

Notice: Undefined property: stdClass::$roles in user_access() (line 820 of […]/Drupal/modules/user/user.module).

Is there anything specific I need to do to avoid this happening (not involving changing log levels or otherwise ignoring the issue)? Any particular best practices when using Drupal queries outside the context of a "real" session? The reason I'm doing this is to get rid of any spurious noise in the logs, and reduce the chance of breaking things in case of change to both my own and Drupal code.

Query code:

$q = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $content_types, 'IN')
    ->propertyCondition('status', 1)
    ->propertyCondition('uid', $uid)
    ->propertyCondition('type', $content_types, 'IN')
    ->fieldCondition('field_something', 'value', $code, '=');

$award_count = $q->count()->execute();

Stack trace:

[…]/Drupal/modules/node/node.module(3334): user_access('bypass node acc...', Object(stdClass))
[…]/Drupal/modules/node/node.module(3296): _node_query_node_access_alter(Object(SelectQuery), 'entity')
[…]/Drupal/includes/module.inc(1163): node_query_entity_field_access_alter(Object(SelectQuery), NULL, NULL, NULL)
[…]/Drupal/includes/database/select.inc(1253): drupal_alter(Array, Object(SelectQuery))
[…]/Drupal/includes/database/select.inc(1261): SelectQuery->preExecute()
[…]/Drupal/includes/database/select.inc(1275): SelectQuery->preExecute()
[…]/Drupal/includes/entity.inc(1389): SelectQuery->execute()
[…]/Drupal/modules/field/modules/field_sql_storage/field_sql_storage.module(652): EntityFieldQuery->finishQuery(Object(SelectQuery), 'entity_id')
[internal function]: field_sql_storage_field_storage_query(Object(EntityFieldQuery))
[…]/Drupal/includes/entity.inc(1216): call_user_func('field_sql_stora...', Object(EntityFieldQuery))
[…]/[my code path]: EntityFieldQuery->execute()

Running Drupal 7.56.

  • can you include your entityfieldquery code? – longboardnode Jul 21 '17 at 15:53

As you can see in the cron doc, cron runs as anonymous user so you have to run the query as admin user.

In the EntityFieldQuery DO doc you can see examples, but essentially you just need to add the addMetaData code to the end of your query:

 $q = $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', $content_types, 'IN')
    ->propertyCondition('status', 1)
    ->propertyCondition('uid', $uid)
    ->propertyCondition('type', $content_types, 'IN')
    ->fieldCondition('field_something', 'value', $code, '=')
    // Run the query as user 1.
    ->addMetaData('account', user_load(1));
  • I ended up using addMetaData('account', user_load($uid)) and testing using drush watchdog-delete and drush watchdog-show. – l0b0 Jul 24 '17 at 14:38
  • @l0b0 just be aware that if you have any access rules for any of the operations, they might not run on a timed cron (ie run cron every 3 hours) because the cron bot is anonymous. So it will work for you if you run cron manually while logged in as admin, but it might not run if it is running on auto). If you are leery of using the admin role "user_load(1)", you can create a "cron" role and give it the needed permissions, then run it as that user. – longboardnode Jul 24 '17 at 16:49
  • This is not for the "Drupal cron", which I have yet to tackle. It's for a custom system cron. – l0b0 Jul 24 '17 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.