tags:

views:

31

answers:

2

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?

A: 

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
Ah, I see. Now I used this code:<?phpif(arg(0) == 'node' //check for node update access if (node_access("update", $node)){return array( 'subject' => t('i am an optional title'), 'content' => 'i am the content');}}?>Still, nothing is returned in the block.
Toxid
you are using hook_block, are you? Or are you using the PHP input filter?
berkes
I'm using the php input filter. Is it a different approach then?
Toxid
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
+1  A: 

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
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