tags:

views:

49

answers:

3

I'm trying to make a variable for an edit button. In template.php, I've put this code

function phptemplate_preprocess_node(&$vars) {

if ($user->uid == $node->uid || in_array('moderator', array_values($user->roles))|| $user->uid == 1){
     $vars['edit'] =  l('Edit', 'node/' . $node->nid . '/edit');          
   }
}

Then I print $edit in the tpl file. It prints an edit link, but the url looks like this: "node/2%Fedit". What am I doing wrong?

+1  A: 

Is it node/2%Fedit or node/%2Fedit? %2F is / encoded as HTML entity. It seems your $node->nid is empty for some reason. Do you have $node available there?

abhaga
Maybe you must use $vars['node'] instead of $node?
berkes
@berkes: Spot on! You should write it as an answer instead of comment so I can confirm.
Toxid
+4  A: 

As abhaga pointed out, the $node variable is not declared. In preprocessors they are set as $vars['node']

In your case, the code would be:

function phptemplate_preprocess_node(&$vars) {

if ($user->uid == $node->uid || in_array('moderator', array_values($user->roles))|| $user->uid == 1) {
     $node = $vars['node'];
     $vars['edit'] =  l('Edit', 'node/' . $node->nid . '/edit');          
   }
}
berkes
+1  A: 

If your code is meant to only show the Edit button to the person who authored the node, there is a much easier way to do this with access permissions.

Turn off Administer Nodes for roles, and assign more granular permissions 'edit any' 'edit own' etc permissions to roles. This will achieve what it looks like you want to do without needing to code.

This way, someone in the Moderator role can -only- edit the node they authored, and you can specify it across content types.

If it is a matter of moving the $tabs/$tabs2 items I believe a theming function exists for them.

Kevin
+1, because indeed, it seems the motivation for the code is wrong: not only is the logic to determine the permission broken, it should and could be done a lot easier. However, that was not the question. Maybe oxic has other reasons for this code, that we do not know of?
berkes
I had tried the node_access('update') before, but it didn't work. Now I realized that it has to be if (node_access('update', $vars['node'])).It is a matter of moving the $tabs items, but I found a way to override the $links variable now, so I'm using it to print edit and delete buttons. It looks a lot cleaner now. Thanks!
Toxid