0

In a custom module, I try to make this code work :

function mymodule_node_access($node, $op, $account) {
  if ($node->type = 'devis' and $op = 'view') {
    $field_language = field_language('node', $node, 'field_project');
    $project_node = node_load($node->fields->field_project[$field_language][0]['value']);
    if ($project_node->uid == $account->uid) {
      return NODE_ACCESS_IGNORE;
    }   
    else {
      return NODE_ACCESS_DENIED;
    }
  } 
}

I get the following notice:

Trying to get a property of a non-object in mymodule_node_access()

I don't know where the problem is.

2 Answers 2

2

$node in hook_node_access() can be either a string for the content-type or a node object. I had to test if it was a string or an object.

if (is_string($node)) { 
  return NODE_ACCESS_IGNORE;
}
else {
  // ...
}
1

Fields are directly on the node object, you're currently trying to find them in another object called fields.

Change this:

$project_node = node_load($node->fields->field_project[$field_language][0]['value']);

To this:

$project_node = node_load($node->field_project[$field_language][0]['value']);

And it should work.

2
  • 1
    Beat me to it. I was also going to add that all fields are attached to a Drupal node object directly as the property field_{field_name}.
    – Shawn Conn
    Sep 7, 2015 at 18:23
  • Thank ou for your fast answer, I changed it and still have the same notice, I really don't know what is the matter Sep 7, 2015 at 18:41

Your Answer

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

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