You are here

function textformatter_field_formatter_view in Text list formatter 7

Implements hook_field_formatter_view().

File

./textformatter.module, line 244
Provide a field formatter to render values as HTML or comma-separated lists.

Code

function textformatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if ($display['type'] == 'textformatter_list') {
    $textformatters = textformatter_field_list_info();
    $settings = $display['settings'];
    $module = $field['module'];
    $element = $list_items = array();
    if (isset($textformatters[$module]) && in_array($field['type'], $textformatters[$module]['fields'])) {
      $function = $textformatters[$module]['callback'];
      if (function_exists($function)) {
        $list_items = $function($entity_type, $entity, $field, $instance, $langcode, $items, $display);
      }
    }
    else {
      foreach ($items as $delta => $item) {
        $list_items = textformatter_default_field_create_list($entity_type, $entity, $field, $instance, $langcode, $items, $display);
      }
    }

    // If there are no list items, return and render nothing.
    if (empty($list_items)) {
      return;
    }

    // CSS classes are checked for validity on submission. drupal_attributes()
    // runs each attribute value through check_plain().
    $classes = explode(' ', $settings['textformatter_class']);
    switch ($settings['textformatter_type']) {
      case 'ul':
      case 'ol':

        // Render elements as one piece of markup and theme as item list.
        $element[0] = array(
          '#theme' => 'item_list',
          '#type' => $settings['textformatter_type'],
          '#items' => $list_items,
          '#attributes' => array(
            'class' => $classes,
          ),
        );
        break;
      case 'comma':

        // Render as one element as comma separated list.
        $element[0] = array(
          '#theme' => 'textformatter_comma',
          '#items' => $list_items,
          '#settings' => $settings,
          '#attributes' => array(
            'class' => $classes,
          ),
        );
        break;
    }
  }
  return $element;
}