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 doin开发者_运维技巧g wrong?
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');
}
}
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?
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.
精彩评论