blocks

Add an "Edit" Tab to Every Block

Last modified: August 18, 2009 - 05:48

This snippet builds on another snippet about "Add an Edit this Block link". Instead of a link this goes further to have the edit link look like the tabs for View, Edit, etc. Here is the code to include in your theme's block.tpl.php file:

<div id="block-<?php print $block->module .'-'. $block->delta; ?>" class="clear-block block block-<?php print $block->module ?>">

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

    <div id="tabs-wrapper">
      <ul class="tabs primary">
        <li class="active">
          <a class="active" href='<?php print check_url(base_path()) ?>admin/build/block/configure/<?php print $block->module;?>/<?php print $block->delta;?>'>Edit</a>
        </li>
      </ul>
    </div>
    <?php
   
endif;
  endif;
?>


<?php if (!empty($block->subject)): ?>
  <h2><?php print $block->subject ?></h2>
<?php endif;?>

  <div class="content"><?php print $block->content ?></div>
</div>

If the core style sheets are being loaded this should produce an "Edit" tab above each block for users who have "administer blocks" privileges.

Create more readable (and portable) block id attributes based on the block title

Last modified: August 18, 2009 - 05:43

Going crazy with a style sheet full of impossible to remember #ids? How about trying to share or reuse styles between sites where the id's aren't the same? We can use the "Block title" field to create friendlier id names like #address instead of #block-block-24.

block.tpl.php

<?php
// Create css id attribute based on the 'Block title' field if available.
if (!empty($block->subject)) {
 
// Make lowercase.
 
$blockid = strtolower($block->subject);
 
// Convert special characters to dashes.
 
$blockid = preg_replace("/([^a-z0-9])/", "-", $blockid);
}
else  {
   
// If no "Block title", create css id attribute the robotic way.
   
$blockid = "block-$block->module-$block->delta";
}
?>

<div class="block<?php print $block->module ? ' block-'.$block->module : '' ?>" id="<?php print $blockid ?>">
  <h2><?php print $block->subject ?></h2>
  <div class="content"><?php print $block->content ?></div>
</div>

The regex and string replacements do a basic job of enforcing legal CSS names. If you're disciplined, you remove them altogether and save some overhead, but this would really limit your block titles assuming you ever display them.

One small thing to should remember when naming your blocks is that in CSS ids can't start with a number — something this particular snippet doesn't account for.

Customize a block title

Last modified: August 18, 2009 - 05:44

This snippet is aimed at someone who wants to change the titles of just one or two core or module blocks using Drupal 4.7. If there are a large number of block titles you want to change, a better approach is to use the locale module to substitute your preferred titles for the default titles.

Note that this functionality is provided through the Admin area in Drupal 5 and later.

To change a specific block title, you can add a conditional statement to your block template file. Here's an example for Drupal 4.7. This is a modification of the standard block.tpl.php that comes with Blumarine or other templates for the phptemplate engine. It changes the name of the "Navigation" block to "My Tools" and the "Who's online" block to "Who's Online".

Syndicate content
 
 

Drupal is a registered trademark of Dries Buytaert.