Add an "Edit this Block" link

I am making a few sites that I won't ultimately maintain and I find people get confused about nodes and books and pages and blocks, so I added an "edit this block" link to the bottom of all custom blocks, visible to users with block admin privs (I stuck pretty much the same thing in my page.tpl.php right after the footer to point users with admin privs to the page where the footer is set.

<div class="<?php print "block block-$block->module" ?>" id="<?php print "block-$block->module-$block->
delta"
; ?>
">
<?php print $block->subject ?>
  <div class="content"><?php print $block->content ?>
  <?php  if ($block->module == "block"):?>
      <?php  if (user_access('administer blocks')) :?>
      <br /><center><a href='/admin/block/edit/<?php print $block->delta;?>'>(edit this block)</a></center>
      <?php endif; ?>
  <?php endif; ?>
  </div>
</div>

Make sure the edit link uses the correct path for your version of Drupal.

  • /admin/build/block/configure for 5.x
  • /admin/block/configure for 4.7
  • /admin/block/edit/ for 4.6

Working code for Drupal 5.1, also with block from custom modules

olio - June 15, 2007 - 12:18

Hi,

this very usefull code snippet above doesn't seem to fit with my version of Drupal (I'm using Drupal version 5.1) and additionally needs a small change for blocks not generated by the block module. Anyway: In case someone had the same problem like me, here is the code with small changes that worked for me.

<div class="<?php print "block block-$block->module" ?>" id="<?php print "block-$block->module-$block->
delta"
; ?>
">
<?php print $block->subject ?>
  <div class="content"><?php print $block->content ?>
<?php
   
if ($block->module == 'block'):?>

<?php  if (user_access('administer blocks')) :?>
<br />
<center>
/** Change the path as follows to get the right link adress in Drupal 5.1*/
<a href='<?php print check_url(base_path()) ?>admin/build/block/configure/<?php print $block->module;?>/<?php print $block->delta;?>'>(edit this block)</a>
</center>
<?php endif; ?>
<?php endif; ?>
</div>
</div>

Paste this code in the file block.tpl.php and everything's fine for the standard blocks. But I ran into another problem: The code above only works for blocks that are coming from the block module. To make it work for blocks generated by your custom modules or - as in my case - generated by the very usefull module 'nodeasblock', you have to change the first 'if'-clause as follows:

...
<?php
   
/** Replace 'nodeasblock' with the name of the module that generated your block  */
   
if ($block->module == 'nodeasblock'):?>

...

To find out which module spits out your specific block, put this line inside your block template (the specific one you are using, e.g. block-right.tpl.php for blocks displayed in the right sidebar, etc.):

<?php
 
print $block->module
?>

Now the name of the module is printed out inside your block. Replace 'nodeasblock' in the example above with this module name you got. That's it.

Another hint for those who are not very familiar with theming blocks: You can theme these blocks individually in a seperate template file with the name: 'block-module_name.tpl.php (in my case 'block-nodeasblock.tpl.php').

I have another version for

bestrong - November 8, 2007 - 09:33

I have another version for the one who use Drupal 5.3 with module views.

<div id="<?php print "block-$block->module-$block->delta"; ?>" class="<?php print "block block-$block->module" ?>">
    <div class="blockinner">
        <?php if ($block->subject): ?>
            <h2 class="title"><?php print $block->subject; ?></h2>
        <?php endif; ?>
        <div class="content">
            <?php print $block->content; ?>
        </div>
    </div>

    <?php
       
if (
            ((
$block->module == "block") && (user_access('administer blocks'))) ||
            ((
$block->module == "views") && (user_access('administer views')))
        ):
   
?>


        <div class="administer">
            <a href="<?php print check_url(base_path()) ?>admin/build/block/configure/<?php print $block->module;?>/<?php print $block->delta;?>">Edit this block</a>
        </div>

    <?php endif; ?>

</div>

 
 

Drupal is a registered trademark of Dries Buytaert.