CustomFormatters.com is the official repository for Custom Formatters, it is still in early development and there is no ability for user submitted content yet, but anyone wanting to get involved can visit https://github.com/Decipher/customformatters.com

 

The following snippets are for the Drupal 7 version of the Custom Formatters module. Use at your own risk. If you find a bug please open an issue in the Custom Formatters issue queue or if you are sure correct the code directly on this page.
Do not post requests in the comments.

Format a text field as a hyperlink

The following PHP formatter appends the content of a text field to a fixed URL to make a hyperlink.

$search_term = $variables['#items'][0]['safe_value'];
$url = 'http://www.streetmap.co.uk/loc/'.$search_term;
$options = array('attributes' => array('target'=>'_blank'));
Return l($search_term, $url, $options);

Format a decimal field showing or not the decimals

The decimal field ("number_decimal") always shows decimals wether or not your number has. For exemple if your number is "20", Drupal shows "20.00". With this custom formatter, Drupal will show the exact number in case the number doesn't have decimals, but will show the decimals if the number actually has.

$output = '';
foreach ($variables['#items'] as $item) {
  if(strpos($item['value'],".") !== false) {
    $output .= trim(trim($item['value'], '0'), '.');
  }
  else {
    $output .= $item['value'];
  }
}
return $output;	

Comments

drupal a11y’s picture

Custom formatters presentation from Drupal Days 2011 Bruessels: http://bxl2011.drupaldays.org/node/303

ppatterson-edc’s picture

I'm trying to remove the leading 0 from some averages, i.e.,

0.345
0.657

I would rather them print out to the screen as

.345
.657

I tried modifying your snippet, but it didn't work - can you help?

$output = '';
foreach ($variables['#items'] as $item) {
if(strpos($item['value'],".") !== false) {
$output .= ltrim($item['value'], "0");
}
else {
$output .= $item['value'];
}
}
return $output;

jag339’s picture

Got a 404 on http://bxl2011.drupaldays.org/node/303. Looks like the page moved.
Found the Custom formatters presentation from Drupal Days 2011 Brussels at:
http://bxl2011.drupaldays.org/bxl2011.drupaldays.org/node/303.html
Cheers!