0

I have some code that checks user access to a certain field using hook_field_access below.

The code runs fine when viewing nodes normally, but throws an error when viewing a node of type 'lesson' through a View:

Notice: Trying to get property of non-object

The error comes from the check for entity type: $entity->type == 'lesson'

If I debug with:

dpm($entity->type);

I get the right value ('lesson'), so the check seems to work.

How can I resolve this error?

function HOOK_utility_field_access($op, $field, $entity_type, $entity, $account) {

  //Check video field only when attached to 'lesson'.
  if ($field['field_name'] == 'field_video' && $entity_type == 'node' && $op == 'view' && $entity->type == 'lesson') {

    //Grant access only if user is logged in.
    if ($account->uid > 0) {
      return TRUE;
    }
    return FALSE;
  }
}
5
  • Are you sure it's not running twice, once when $entity is populated and once it isn't?
    – Clive
    Commented Jun 18, 2014 at 14:15
  • @Clive - I think that's it. If I move dpm($entity); to the top, it's actually invoked 5 times, and the first time the entity is empty. Should I just add If ($entity); somewhere?
    – Dave Bruns
    Commented Jun 18, 2014 at 14:38
  • is_object() might be better but yeah, that would do it. Sometimes these hooks get called for new entities, and $entity isn't available yet. Or there may be a dodgy field in your install somewhere which is lacking an entity when it should have one in this context. If you're not up for a serious debug sesh then yeah, bandage it up :)
    – Clive
    Commented Jun 18, 2014 at 14:39
  • Adding if (is_object($entity)) worked, thanks. Housekeeping: is it recommended to add this as an answer and then accept, in order to close out the question?
    – Dave Bruns
    Commented Jun 18, 2014 at 14:55
  • I'd go so far as to say it's encouraged :)
    – Clive
    Commented Jun 18, 2014 at 15:16

2 Answers 2

2

Based on @clive's comment above, I adjusted the code to check that $entity is actually present. I'm not sure if this is a problem unique to my install or not. I only saw the error when viewing a node of type 'lesson' through a paged view that depends on some custom relationship code, so entirely possible the error is mine somewhere in a custom module.

function HOOK_field_access($op, $field, $entity_type, $entity, $account) {

      //Check to make sure we have an entity. Abort if not.
      if (is_object($entity)) {

        //Check video field only when attached to 'lesson'.
        if ($field['field_name'] == 'field_video' && $entity_type == 'node' && $op == 'view' && $entity->type == 'lesson') {

          //Grant access only if user is logged in.
          if ($account->uid > 0) {
            return TRUE;
          }
          return FALSE;
        }
      }
    }
1

I think your conditional statement won't work as it is. You are checking for $entity_type to be 'node' and $entity->type to be 'lesson' and I feel that's a bit conflicted.

Also, if I understand the API correctly, $entity is optional, so it's probably better to check $entity_type:

if ($field['field_name'] == 'field_video' && $entity_type == 'lesson' && $op == 'view')

or maybe check that $entity is an object before you check the value.

3
  • $entity is definitely not optional. But there's nothing to stop a caller passing it as NULL or whatever. Also the $entity_type is the type of entity (node, user, taxonomy_term, etc). $entity->type in this context is equivalent to $node->type, which is querying the bundle of the node (its type). They're not the same thing so you can use one to check for the other
    – Clive
    Commented Jun 18, 2014 at 14:36
  • I stand corrected, I was ignoring the scope. However the API lists $entity as "(optional)", but it probably means "possibly NULL".
    – Nels
    Commented Jun 18, 2014 at 15:50
  • Yeah I just noticed that actually - seems like the params themselves should be optional in code if that's true. I guess either the docs or the code could be wrong, but one of them is
    – Clive
    Commented Jun 18, 2014 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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