Add an "Edit this Block" link

Last modified: August 23, 2009 - 19:25

Adding an "Edit this Block" to the bottom of custom blocks makes the blocks easier to administer, especially for site administrators who are not familiar with administering blocks in Drupal. This code snippet adds a "edit this block" link to the bottom of custom blocks and displays the link to users with the "administer blocks" permission. A modification of this can also be used in page.tpl.php to link to the administration settings for changing the footer.

<div class="block-<?php print $block->module; ?>" id="block-<?php print $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')) :?>
      <div><a href='/admin/block/edit/<?php print $block->delta;?>'>(edit this block)</a></div>
      <?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

A similar code snippet for Drupal 6 is also available.

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>

Here's mine

konsumer - July 26, 2009 - 21:32

Mine uses destination (it will go back to the current page when user is done editing), a nice link title, and sets the class for easy CSS theming (I like to use little CSS bg images, rather then text). Insert this into block.tpl.php, wherever you want the link. This is for Drupal 6:

<?php
if (user_access('administer blocks') && $block->module == "block"){
  print
l('edit', 'admin/build/block/configure/block/' . $block->delta, array('attributes'=>array('class'=>'block_edit', 'title'=>'edit '.$block->subject.' block'), 'query'=>drupal_get_destination()));
}
?>

The CSS I use looks something like this:

.block_edit {
  width: 32px;
  height: 32px;
  background: #fff url(../images/edit_icon.png) top left no-repeat;
  text-align:left;
  text-indent: -1000px;
}

finally, I was about to post

seutje - August 19, 2009 - 10:27

finally, I was about to post a "why doesn't any1 use l()?" :P

but I prefer to keep logic out of my .tpl.php files, so I put this in my template.php to have the block content be prepended with the edit link

<?php
function  phptemplate_preprocess_block(&$vars) {
 
$block = $vars['block'];
  if (
user_access('administer blocks') && $block->module == "block"){
   
$vars['block']->content = l(t('edit'), 'admin/build/block/configure/block/' . $block->delta, array('attributes'=>array('class'=>'block_edit', 'title'=>'edit '.$block->subject.' block'), 'query'=>drupal_get_destination())). $vars['block']->content;
  }
}
?>

CSS:

.block_edit {
  float:right;
  margin-top:-3em;
}

 
 

Drupal is a registered trademark of Dries Buytaert.