You are here

field_count_formatter.module in Field Count Formatter 7

Same filename and directory in other branches
  1. 8 field_count_formatter.module
  2. 2.x field_count_formatter.module

Simple module to provide a formatter for all field types to show the number of items entered into the field. This is most useful for multiple value fields.

File

field_count_formatter.module
View source
<?php

/**
 * @file
 *
 * Simple module to provide a formatter for all field types to show the number of items
 * entered into the field. This is most useful for multiple value fields.
 */

/**
 * Implements hook_field_formatter_info().
 */
function field_count_formatter_field_formatter_info() {
  $types = field_info_field_types();
  return array(
    'count' => array(
      'label' => t('Count'),
      'field types' => array_keys($types),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function field_count_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  // ... In case we add another formatter.
  if ($display['type'] == 'count') {
    $element[0]['#markup'] = count($items);
  }
  return $element;
}