Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Custom field formatter in Drupal 7

Parent Feed: 

This article is an example of how to add a field formatter to the field, this is been achieved through the field formatter hooks.

Field formatters are used to render fields in node views. Drupal provides default formatters.
To customize the output of a field and have it apply it to multiple fields of the same type, a custom formatter needs to be created.

To start look at the default formatters available for the link field in a Content Type. In Drupal 7 the available formatters for a field are found by going to Structure > Content types > The content type > Manage display. and you can see here list field formatters for each field.

To create a custom field field formatter.
1. Defining a Field Formatter.
2. Themeing the Field Formatter.

1. Defining a Field Formatter

To define a formatter in Drupal 7, two hooks need to be called: hook_field_formatter_info() and hook_field_formatter_view().

<?php
/**
* Implements hook_field_formatter_info().
*/

function mymodule_field_formatter_info() {
  $info = array(
    'website' => array(
      'label' => t('My module url.'),
      'field types' => array('link_field'),
      'description' => t('A customized link for My module only.'),
    ),
  );
  return $info;
}

?>

The field types represents the field type, currently this is a link field.

<?php
/**
* Implements hook_field_formatter_view().
*/
function mymodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'website':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'mymodule_field_formatter',
          '#title' => $entity->field_website['und'][0]['title'],
          '#url' => $entity->field_website['und'][0]['url'],
        );
     }
      break;
  }
  return $element;
}
?>

 

The #title and #url are the values which would be accessable for the template file.
And #theme value mymodule_field_formatter would be the theme variable which would be themed in hook_theme.
 

2. Themeing the Field Formatter

To theme the formatter, the hook_theme() needs to be implemented. This is where the template file is called.

<?php
/**
* Implements hook_theme().
*/
function mymodule_theme() {
  return array(
    'mymodule_field_formatter' => array(
      'template' => 'mymodule_field_formatter',
      'variables' => array(
        'title' => NULL,
        'url' => NULL,
      ),
    ),
  );
}
?>

The mymodule_field_formatter is the name of the template, which needs to be implemented, so this file name would be like mymodule_field_formatter.tpl.php
this template file would contain the following code.

<a href="http://dev-karthikkumardk.pantheonsite.io/Custom-field-formatter-in-Drupal7/<?php print $url; ?>"><?php print $title; ?></a>
<div class="website-title"><?php print $url; ?></div>

Thats it, the new field formatter "My module url." would be created in the link field formatters list, which can be used.
This field formatter can also be used in the views.
And another hook, hook_field_formatter_prepare_view which can be used to alter the rendering of the field.

Thank You..!

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web