Hi all, I have a block displaying a random image from my galleries. I would like the block title to be a link to the main gallery page, and if possible to look exactly like the other block titles (that is, not like a link).

The website is here

The block title (PYRN GALLERY) would lead to the publications/gallery page. Not sure how to do this though.

Comments

gpk’s picture

I think if you copy your theme's block.tpl.php to block-xx.tpl.php, where id is the block's id number, then you can edit that template and it will only be used for that block.

You should find a part in the template something like

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

and you would need to change the middle line to something like

  <h2><a href="<?php print base_path(); ?>publications/gallery"><?php print $block->subject ?></a></h2>
gostram’s picture

Thanks a lot gpk, I also found this node which seems to be relevant: http://drupal.org/node/74481

The only issue is that I seem to have several blocks with the same number, I guess because of the type of block.

My block's type seems to be "image" and its delta "1"

now do I just create a block-01.tpl.php file with the code, upload it in the theme folder and it will work automatically? how do I take into account the type of block?

Thanks in advance for your answers

joachim’s picture

The template filename needs to be block-[module]-[delta].tpl.php
So in your case, block-image-1.tpl.php

gostram’s picture

woaaaaaaaaaaaaah!!!!!! thanks Joachim. That definitely helped. Sometime I wished there'd be more basic explanation like the one you just gave on drupal.org

Most of the time, the stuff I find are "use this code" or "tweak the patch to adjust X to Y". Really hard to understand....

thanks again.

gpk’s picture

Thanks for setting me right, joachim! And for sorting out this page http://drupal.org/node/104319 which describes it all in full (I couldn't find it yesterday - nice job).

gpk
----
www.alexoria.co.uk

adaven’s picture

If you want to apply this to all blocks, you can create a small modules called say, block_titles, with the following function:

<?php
/**
 * Implementation of HOOK_preprocess_block
 * 
 * Drupal allows us to store html in the block title, but check_plain() strips it out :(
 * To reverse this we put it back in again directly from the database. 
 * 
 * WARNING: potential security risk if you don't trust people who can create blocks.
 * 
 */
function block_titles_preprocess_block(&$variables) {
  $subject = db_result(db_query("SELECT title from {blocks} where bid=%d", $variables['block']->bid));
  if($subject) {
    $variables['block']->subject = $subject;
  }
}
?>

If your worried about security, then you could always define a block_titles_form_block_admin_configure_alter function that altered the block form, allowing users with the right access permission to tick a 'HTML title' tick box, but then you'd need to store these values in a table, and alter the above function to check these values. Not hard, but unnecessary in most cases.

gcassie’s picture

Here is another approach that I've used: http://drupal.org/node/28537

decomposer’s picture

Thanks to all who posted here. For newbies like myself, to get the module and delta values, it's less confusing to look at the source if you're logged in as a user, not admin.

I wanted users to be able to click on the "Who is Chatting" block title to join the chat. This has made a world of difference in the number of users chatting.

The block.tpl.php (found in sites/all/themes/(name of the theme) was this:

<div class="block block-<?php print $block->module ?>" id="block-<?php print $block->module ?>-<?php print $block->delta ?>">
    <h2 class="title"><?php print $block->subject ?></h2>
    <div class="content"><?php print $block->content ?></div>
 </div>

I viewed the source of a node where the "Who is Chatting" block was active and found the id for this block to be phpfreechat-0. Then I copied block.tpl.php, renaming the copy as block-phpfreechat-0.tpl.php. It stays in the same directory as block.tpl.php.

Then I edited block-phpfreechat-0.tpl.php as follows:

<div class="block block-<?php print $block->module ?>" id="block-<?php print $block->module ?>-<?php print $block->delta ?>">
    <h2 class="title">

<a href="<?php print base_path(); ?>node/4753"><?php print $block->subject ?><br />(Click here to chat)</a>

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

Of course, you'd change node/4753 to the relative link you want.

This works just like I wanted it to -- hope this helps someone.

vigilanz’s picture

you can also simply change

$block->subject = $block->title == '' ? '' : check_plain($block->title);

to

$block->subject = $block->title == '' ? '' : $block->title;

in modules/block/block.module (although i'd consider this a hack
and prefer the hook solution).

if 64 characters (including the html) are not enough, make it
somewhat bigger in the database, for postgres that is

alter table blocks alter column title type varchar(128)

e-roula’s picture

i was searching for a way to make a block title link to another page. and this page and the other link http://drupal.org/node/104319 were very helpful.

i just think that there should be an easier way to do this without modifying the code, i mean from the block admin interface, like when configuring a block in the block title field we should be able to add a link there. i hope the next Drupal releases would include this small update in them, i dont know if it's important to others as well but i think it's a very useful feature.

thank you all for your help...

entr3p’s picture

If anyone is looking for a module to do this, there's a module now for that.

http://drupal.org/project/block_titlelink

jonne.freebase’s picture

What would it take to backport this module to Drupal 5? Would this just work if I just change the version numbers in the .info file, or are there too many changes between 5 and 6 to make it practical? I'm running a website that I can't just upgrade as it uses modules that don't exist in 6 yet.

edit: well, tried doing exactly that on a local copy of the site, and had disastrous results. Seems like the whole i18n system changed between both versions.

benleivian’s picture

Thanks!

meeta’s picture

excellent solution!