I did a formatter (code) to work with tokens. I needed that just because I dont know how to get the base_path in basic mode (is it possible? is there a token to the base_path?).

$code = '<a class="colorbox colorbox-insert-image" href="' . base_path() . '[filefield-filepath]" rel="gallery-all" title="[filefield-title]"><img alt="[filefield-alt]" class="imagecache imagecache-post legendacinza" src="' . base_path() . '[filefield-imagecache-post]" style="float: right;" title="[filefield-title]" /></a>';

$formatter = is_object($element['#formatter']) ? $element['#formatter'] : custom_formatters_formatter($element['#formatter']);
$formatter->code = $code;

return _custom_formatters_token_replace($formatter, $element);

Is this correct? Is this the best approach?

Is it possible to have this feature natively?

Thanks!!!

[EDITED - Typo, grammar, etc...sorry, english is not my first language]

Comments

soulfroys’s picture

Oh boy... works only with the first loaded image. Returning to my "hard" coded without tokens:

$path = base_path() . $element['#item']['filepath'];
$path_imagecache = base_path() . imagecache_create_path('post', $element['#item']['filepath']);
$title = $element['#item']['data']['title'];
$alt = $element['#item']['data']['alt'];

$output = '<a class="colorbox colorbox-insert-image" href="' . $path . '" rel="gallery-all" title="' . $title .'"><img alt="' . $alt .'" class="imagecache imagecache-post legendacinza" src="' . $path_imagecache . '" style="float: left;" title="' . $title .'" /></a>';

return $output;
iantresman’s picture

I expressed a similar request in my post "Custom Formatters: use token in PHP".

Perhaps all that is required is a simple explanation of how to produce the appropriate PHP code from a token. If I know that the token is "[node-title]", how can I either

  1. Lookup the equivalent PHP coding
  2. Is there a function that converts a token into usable PHP code.

    ie. $nodetitle = convtoken("[node-title]");

It seems that "Basic" mode has access to tokens, so I presume that it uses a snippet to convert from tokens to something more usable in PHP?

soulfroys’s picture

Title: Allow tokens in advanced mode » Allow tokens in advanced mode
Status: Active » Closed (works as designed)

I spoke too soon... the "Convert" option does just that. :)

The correct syntax is:

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

$formatter->code = "<a class=\"colorbox colorbox-insert-image\" href=\"" . base_path() . "[filefield-filepath]\" rel=\"gallery-all\" title=\"[filefield-title]\"><img alt=\"[filefield-alt]\" class=\"imagecache imagecache-post legendacinza\" src=\"" . base_path() . "[filefield-imagecache-post_maximo]\" style=\"float: right;\" title=\"[filefield-title]\" />";

return _custom_formatters_token_replace($formatter, $element);
iantresman’s picture

Well spotted! For the benefit of others, when you create a new BASIC custom format and list it in the summary table, one of the operations is "Convert". This converts a BASIC custom format into the equivalent ADVANCED PHP format, with the necessary code.

Unless you create your own BASIC formatter, you'd never know there was Convert option.

iantresman’s picture

Issue summary: View changes

Typo, grammar, etc...sorry, english is not my first language.