1

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?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Toxid
  • 615
  • 10
  • 18

2 Answers2

1

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.

marcvangend
  • 5,592
  • 4
  • 22
  • 32
  • I see the problem now, I use a module to ajaxify the block. It outputs it empty for some reason. Your code worked when I disabled the module. – Toxid Jul 19 '10 at 16:16
0

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

berkes
  • 26,996
  • 27
  • 115
  • 206
  • Ah, I see. Now I used this code: t('i am an optional title'), 'content' => 'i am the content'); }} ?> Still, nothing is returned in the block. – Toxid Jul 18 '10 at 13:12
  • you are using hook_block, are you? Or are you using the PHP input filter? – berkes Jul 18 '10 at 13:32
  • I'm using the php input filter. Is it a different approach then? – Toxid Jul 18 '10 at 13:37
  • If you created a block at /admin/build/block/add and used the php input filter to generate the block contents, you should indeed print, not return. Creating a block in a module with hook_block is a different approach and requires a return just like berkes says. – marcvangend Jul 18 '10 at 22:10