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.
Doing this in template.php give Drupal better performance and 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;
}
}
?>
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 by using hook_preprocess_content_field().
No magic here, but notice how we position the transparent background. For this to work you have to make sure that the field-item you are placing the text on is relative positioned. By doing so are we able to use absolute position the text, relative to the parent object..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!
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.
Add new comment