09.11.2011 - Fixed error in template.php
This how to will to point you the right direction, hell even providing you with an solution to how to add caption to your image fields. No magic, no extra modules. We simply take usage of the title field that is a part of imagefield.

As usual in Drupal are there is multiple ways of doing tings and this is no different. This is two way of solving this in Drupal 6 but rest assure there are others out there.
Skip this section if you already got a working image field. Before we get into the code do we need to configure the basic settings imagefield and imagecache. Install and filefield module, and add an imagefield to one of your content types (node type).

Make sure you enable the title tag in your imagefield. I normally also change the title field to a "textarea".
Install imagecache and create a preset that you enable in your newly created imagefield display settings. Verify that it is working as expected before proceeding.
<?php if (!$field_empty) : ?>
<div class="field field-type-<?php print $field_type_css ?> field-<?php print $field_name_css ?>">
<?php if ($label_display == 'above') : ?>
<div class="field-label"><?php print t($label) ?>: </div>
<?php endif;?>
<div class="field-items">
<?php $count = 1;
foreach ($items as $delta => $item) :
if (!$item['empty']) : ?>
<div class="field-item <?php print ($count % 2 ? 'odd' : 'even') ?>">
<?php if ($label_display == 'inline') { ?>
<div class="field-label-inline<?php print($delta ? '' : '-first')?>">
<?php print t($label) ?>: </div>
<?php } ?>
<?php print $item['view'] ?>
<?php if ($page == 1): ?>
<?php if ($item['data']['title']): ?>
<div class="image-caption"></div>
<div class="image-caption-text"><?php print $item['data']['title']; ?></div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php $count++;
endif;
endforeach;?>
</div>
</div>
<?php endif; ?>
The only changes I did to the original file was adding these lines.<?php if ($page == 1): ?> // make sure we are not in teaser mode (build mode)
<?php if ($item['data']['title']): ?> // Make sure that imagefield title actually contain data.
<div class="image-caption"></div>
<div class="image-caption-text"><?php print $item['data']['title']; ?></div>
<?php endif; ?>
<?php endif; ?>
The example above enables this feature to every content type where you reuse this imagefield, so if this is not wanted behavior, and you need to check what content type this is before printing out the image caption.
That is all you need to do. You should be able to see the title text and the background. They are not correctly positioned or event transparent but we are very very close. All we now need is a little CSS love.
If your site is getting a lot of traffic are will this way of doing give you better front end speed pluss you you avoid littering your theme with multiple tpl.php –files.
We are overriding the function - theme_preprocess_content_field().<?php
function theme_preprocess_content_field(&$variables) {
if ($variables['field_name'] == 'field_image' & $variables['page']) { // Make sure that the field is present and we are viewing full page view.
$element = $variables['element'];
if(!$element['items'][0]['#item']['data']['title'] == '') { // Make sure that the title field is not empty.
$image_caption = '<div class="image-caption"></div><div class="image-caption-text">' . $element['items'][0]['#item']['data']['title'] . '</div>';
$variables['items'][0]['view'] = $element['items'][0]['#children'] . $image_caption;
}
}
?>
That is all you need to do. The end result should be the same as the one presented in the first example. This code could also be moved out of template.php and into your own mini-module.
Nothing special here, except how the transparent background is positioned. For this to work you have to make sure that the field-item we are placing the text on is relative positioned. This enable us to absolute position the text. This CSS works is used on a imagecache preset that create images with 800px width.
.field-field-image .field-item {
position: relative;
}
.image-caption {
position: absolute;
padding: 20px 10px;
width: 780px;
bottom: 5px;
height: 30px;
background: #000;
opacity: 0.3;
}
.image-caption-text {
position: absolute;
padding: 20px 10px;
width: 780px;
bottom: 0;
color: #eee;
}
Comments
Yngve Bergheim (not verified)
Tue, 12/21/2010 - 5:55pm
Permalink
Grundig og god beskrivelse!
Grundig og god beskrivelse!
webmaster
Thu, 01/06/2011 - 1:31am
Permalink
Takk. Får ta meg tid å
Takk. Får ta meg tid å finskrive del II om bruk av template.php og kansje se hvor forskjellig D7 er når du skal gjøre noe lignende, tross alt er det Drupal 7 dag!
IncoffSoara (not verified)
Wed, 03/09/2011 - 10:23pm
Permalink
This actually answered my
This actually answered my problem, thanks!
mamba (not verified)
Tue, 11/22/2011 - 1:38pm
Permalink
Drupal7 for a solution?
Drupal7 for a solution?
psycho (not verified)
Sat, 11/26/2011 - 12:44pm
Permalink
I'm also looking for D7
I'm also looking for D7 solution.
Anonymous (not verified)
Tue, 12/20/2011 - 2:35am
Permalink
I want to thank you for this
I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.
http://www.minmetalschina.com/
Magento Web Designer (not verified)
Mon, 01/02/2012 - 9:54am
Permalink
Frnalky I think that’s
Frnalky I think that’s absolutely good stuff.
Web Designer (not verified)
Sat, 02/04/2012 - 12:01pm
Permalink
Drupal is complex.
Drupal is complex.
This solution of yours is pretty handy and useful.
Thanks for posting.
MartinV (not verified)
Wed, 02/08/2012 - 11:18pm
Permalink
I am glad I found your
I am glad I found your website Stein, looking forward to read more
Martin @ http://www.neojogos.com
Add new comment