1

I'm using Display Suite custom fields to have my Edit link rendered somewhere else on the node page, I'd like to see if there's an easy way to check if the current user has the permission to edit the node or not.

So far I tried this:

<?php 
global $user;
      if (node_access('update', $nid, $user) == true) {
       echo l(t('Edit this node'), 'node/' . $node->nid . '/edit');
      }
?>

unfortunately does not work, Thanks a million.

7
  • Why not check with the role of the user?
    – Aparna
    Jul 22 '15 at 10:33
  • there maybe a lot of different roles, and I'm not sure if in future I'm going to have any new roles, so I rather to stick with the raw permission in case I added a new role later on down the road!
    – user23288
    Jul 22 '15 at 10:35
  • Could you please try to put full $node object in node_access function?
    – Andrew
    Jul 22 '15 at 10:47
  • would you please elaborate more since I barely understand any php!
    – user23288
    Jul 22 '15 at 10:48
  • Replace $nid with $node @Gus
    – Clive
    Jul 22 '15 at 10:59
2
+50

First of all you need to make sure to get access to your administrators as well as the author, also note that you need to load nid along with the uid

<?php 
global $user;
$node = node_load(arg(1));
if(user_access('edit any "" content') || ($user->uid == $node->uid)){
    echo l(t('Edit this node'), 'node/' . $node->nid . '/edit');
}
?>
1
  • 1
    This should be correct, but I believe global $user; & $node = node_load(arg(1)); are not necessary. I think the if statement should be if (user_access('edit own "" content') || user_access('edit any "" content')) { the user_access function has access to the global $user object Jul 28 '15 at 11:47
1

It might be that you don't have access to the node object, in which case changing to the following should work...

<?php 
  global $user;
  if ($node = node_load(arg(1))) {
    if (node_access('update', $node, $user) == true) {
      echo l(t('Edit this node'), 'node/' . $node->nid . '/edit');
    }
  }
?>

Your Answer

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