You are here

function fft_field_formatter_view in Field Formatter Template 7

Implements hook_field_formatter_view().

File

./fft.module, line 250
Field formatter template.

Code

function fft_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  $data = array();
  switch ($field['type']) {
    case 'image':
    case 'file':
      $data = $items;
      foreach ($data as $key => $item) {
        $data[$key]['path'] = file_create_url($item['uri']);
        if ($field['type'] == 'image') {
          for ($i = 1; $i <= 2; $i++) {
            if (!empty($settings["image_style_{$i}"])) {
              $data[$key]["path_{$i}"] = fft_field_styled_image_url($item['uri'], $settings["image_style_{$i}"]);
              $dimensions = array(
                "width" => $item['width'],
                "height" => $item['height'],
              );
              image_style_transform_dimensions($settings["image_style_{$i}"], $dimensions);
              $data[$key]["width_{$i}"] = $dimensions["width"];
              $data[$key]["height_{$i}"] = $dimensions["height"];
            }
          }
        }
      }
      break;
    case 'text':
    case 'text_long':
    case 'text_with_summary':
      foreach ($items as $key => $item) {
        $data[] = array(
          'text' => $item['safe_value'],
        );
      }
      break;
    case 'taxonomy_term_reference':
      foreach ($items as $key => $item) {
        $term = taxonomy_term_load($item['tid']);
        $data[] = (array) $term;
      }
      break;
    case 'field_collection':
      $ids = field_collection_field_item_to_ids($items);
      $data = field_collection_item_load_multiple($ids);
      break;
    case 'entityreference':
      $ids = array();
      foreach ($items as $key => $item) {
        $ids[] = $item['target_id'];
      }
      $data = entity_load($field['settings']['target_type'], $ids);
      break;
    default:
      $data = $items;
      break;
  }
  if (!empty($data)) {
    $template_file = fft_store_dir() . "/" . $settings['template'];
    $settings_extras = drupal_parse_info_format($settings['settings']);
    $attached = array();
    foreach (array(
      'js',
      'css',
      'library',
    ) as $item) {
      if (isset($settings_extras[$item])) {
        foreach ((array) $settings_extras[$item] as $key => $value) {
          if (is_string($value)) {
            $attached[$item][] = fft_realpath($value, $template_file);
          }
          else {
            $attached[$item][] = $value;
          }
        }
      }
    }
    $settings_extras = $attached + $settings_extras;
    $settings_extras['image_style_1'] = $settings['image_style_1'];
    $settings_extras['image_style_2'] = $settings['image_style_2'];
    $output = fft_render($template_file, array(
      'data' => $data,
      'entity' => $entity,
      'settings' => $settings_extras,
    ));
    $element[0] = array(
      '#markup' => $output,
      '#attached' => $attached,
    );
  }
  return $element;
}