I have created a custom formatter with the following code:

<div class="image-with-caption">
<div class="image"><img src="[site-url]/[filefield-imagecache-square-thumbnail]" alt="[filefield-alt]" title="[filefield-title]" /></div>
<div class="caption-box"><span="caption"><a href="[site-url]/node/[node-nid]">Description: [field_image-filefield-description] Size: [field_image-filefield-filesize_formatted]</a></span></div>
</div>

when using insert to insert an image with the above formatter, it will render into the following:

<div class="image-with-caption"> 
<div class="image"><img src="http://www.example.com:8080/sites/www.example.com/files/imagecache/square-thumbnail/Albert_Einstein_Head_Cleaned_N_Cropped.jpg" alt="" title=""  width="90" height="90"/></div> 
<div class="caption-box"><span="caption"><a href="http://www.example.com:8080/node/1">Description: Albert Einstein Description Size: 585.14 KB</a></span="caption"></div> 
</div></div> 

The code changes some of the tokens, but not all. I have inserted the same code through insert and it is able to format the above two missing tokens correctly. Though it has a similar problem with the alt and title tokens.

As the example shows, the alt and title tags are not rendered; the same happens if I change those tags to [field_image-filefield-alt] and [field_image-filefield-title]

Comments

boclodoa’s picture

After some headaches here is my solution:
use an advanced custom formatter with the following code

// Parse tokens.
$formatter = is_object($element['#formatter']) ? clone $element['#formatter'] : clone custom_formatters_formatter($element['#formatter']);
$formatter->code = "<div class=\"image-with-caption\">
<a href=\"[site-url]/[filefield-filepath]\" rel=\"lightbox\" alt=\"[filefield-alt]\" title=\"[filefield-title]\">
<div class=\"image\"><img src=\"[site-url]/[filefield-imagecache-thumbnail]\" alt=\"[filefield-alt]\" title=\"[filefield-title]\" /></div>
</a>
<div class=\"caption-box\"><span=\"caption\"><a href=\"[site-url]/[filefield-filepath]\" rel=\"lightbox\" >";
$description = "";
$title = "";
$imgs = $element['#node']->field_imagen;
foreach ($imgs as $key => $value) {
   if ($value['fid'] == $element['#item']['fid'] ) { 
       $description = $value['data']['description'];
       $title = $value['data']['title'];
       break;
   }
}
$formatter->code .= "<b>$title".".</b> ";
$formatter->code .= $description;
$formatter->code .= "</a></span></div>
</div>";
return _custom_formatters_token_replace($formatter, $element);

How did I get here? using the "convert" option of the custom formatter over the formatter posted by pkej, and then analyzing the output of an other (advanced) custom formatter with the code

return print_r($element);

I've notice two things:
1 - the $element array does not have the same content if you call it trough the insert module, or trough the cck "display fields", or through the the linodef module.
2 - the $element['#item']['data'] is empty when you call it through the linodef module.

WARNING: this code will give you a server error if you don't change the name of my imagefield (field_imagen) with yours, and the name of my imagecache preset (thumbnail) with yours.

boclodoa’s picture

an other example with more details

// Parse tokens.
$formatter = is_object($element['#formatter']) ? clone $element['#formatter'] : clone custom_formatters_formatter($element['#formatter']);

$description = "";
$title = "";
$imgs = $element['#node']->field_figure;
foreach ($imgs as $key => $value) {
   if ($value['fid'] == $element['#item']['fid'] ) { 
       $description = $value['data']['description'];
       $title = "Figure ".$key;
       break;
   }
}
global $base_url;
$imagecachepath = $base_url."/system/files/imagecache/bigfigure/Figures/";
$imagecachepath .= $element['#item']['filename'];

$imagecachepath2 = $base_url."/system/files/imagecache/thumbnail/Figures/";
$imagecachepath2 .= $element['#item']['filename'];

$rel = "lightbox[field_figure][<b>$title.</b> $description <br /><br /><a href=\"[site-url]/[filefield-filepath]\" target=\"_blank\" id=\"lightbox2-download-link-text\">Download Original</a>]";

$rel = htmlspecialchars($rel);

$formatter->code = "<div class=\"image-with-caption\">
<a href=\"$imagecachepath\" rel=\"$rel\" alt=\"[filefield-alt]\" title=\"[filefield-title]\">
<div class=\"image\"><img src=\"$imagecachepath2\" alt=\"[filefield-alt]\" title=\"[filefield-title]\" /></div>
</a>
<div class=\"caption-box\"><span=\"caption\"><a href=\"$imagecachepath\" rel=\"$rel\" >";

$formatter->code .= "<b>$title".".</b> ";
$formatter->code .= $description;
$formatter->code .= "</a></span></div>
</div>";
return _custom_formatters_token_replace($formatter, $element);
pkej’s picture

Thank you very much for your time and effort. I will use it for future reference on my upcoming play time with D7 :)