Image

Field Formatter

Field Formatter is an integral part of Drupal’s built-in functionality, helping developers design and manage interactive, content-driven websites. It aids in providing an appropriate output of field data by ‘formatting’ it according to the defined specifications.

Drupal, being a flexible and expansive content management system, offers a robust range of out-of-the-box field types that can be formatted in myriad ways, including text, images, date, and email. For instance, an image field can be formatted to display the image as a thumbnail, a medium, or a large image. Likewise, a text field could be formatted to display as plain text, trimmed or full text.

Consider an example where we’re defining a custom field formatter for a text field. In Drupal 8 and 9, you can create a custom field formatter by implementing hook_field_formatter_info():

function my_module_field_formatter_info() {
  return array(
    'my_custom_formatter' => array(
      'label' => t('My Custom Formatter'),
      'field types' => array('text'),
    ),
  );
}

This will inform Drupal about your new custom field formatter.

Afterward, we can proceed by specifying how the field data will be rendered by implementing another method, the hook_field_formatter_view().

function my_module_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    if ($display['type'] == 'my_custom_formatter') {
      // Apply whatever formatting or processing you want on field item values.
      $element[$delta] = array('#markup' => custom_process_function($item['value']));
    }
  }
  return $element;
}

The hook_field_formatter_view() function provides the formatted output of the field to be rendered in the front-end.

Field Formatters, hence, prove to be a powerful tool in Drupal Development, giving developers the freedom to format and display field data as per their website’s requirement. It not only keep things structured and maintainable, but also enables you to extend Drupal’s functionality according to your project’s needs.

Latest Posts
↑ Top