Question

I have a block that I want to show edit and delete buttons for users with access, and other buttons for the rest of the users. This is the script I'm using for the users with update permission:

    <?php
if(arg(0) == 'node' && is_numeric(arg(1))){
    //load $node object
    $node = node_load(arg(1));
    //check for node update access
    if (node_access("update", $node)){
print '<p><a href=\"./edit\">edit</a> <a href=\"./delete\">delete</a></p>';

}}
?>

This is the same script I used for block visibility on other blocks, and it works. Why isn't it working here?

Was it helpful?

Solution

I tested your code on a Drupal 6 site and it seems to work fine, except for the link urls it creates. I do see two links named "edit" and "delete". Are you sure that you enabled the block in a region, and that the region is displayed in page.tpl.php? (You can check that by putting another block in the same region and see if it does show up.)

To get the correct links, I recommend to use Drupal's l() function like this:

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
  //load $node object
  $node = node_load(arg(1));
  //check for node update access
  if (node_access("update", $node)){
    $nid = $node->nid
    print l(t('edit'), "node/$nid/edit") .' '. l(t('delete'), "node/$nid/delete");
  }
}
?>

Note that I'm also using the t() function to make "edit" and "delete" translatable.

OTHER TIPS

A block should not print but return. And it should return an array:

return array(
  'subject' => t('i am an optional title'),
  'content' => 'i am the content');

http://api.drupal.org/api/function/hook_block/6

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top