You are here

function ds_field_formatter_view in Display Suite 7.2

Same name and namespace in other branches
  1. 7 ds.module \ds_field_formatter_view()

Implements hook_field_formatter_view().

File

./ds.module, line 1214
Display Suite core functions.

Code

function ds_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  global $language;
  $element = array();
  if ($display['type'] === 'ds_taxonomy_view_mode') {
    $view_mode = $display['settings']['taxonomy_term_reference_view_mode'];
    foreach ($items as $delta => $item) {
      if ($item['tid'] == 'autocreate') {

        // We don't necessarily have a link when this is
        // an autocreated term, usually in preview.
        // So just send the term as check plained markup.
        $build['#markup'] = check_plain($item['name']);
      }
      else {
        $term = taxonomy_term_load($item['tid']);
        if (module_exists('i18n_taxonomy')) {
          $term = i18n_taxonomy_localize_terms($term);
        }
        if (!is_object($term)) {

          // This term is missing in the database.
          continue;
        }
        $settings = $display['settings'];
        if ($settings['use_content_language'] && !empty($GLOBALS['language_content']->language)) {
          $langcode = $GLOBALS['language_content']->language;
        }
        if (!empty($term)) {
          $build = taxonomy_term_view($term, $view_mode, $langcode);
        }
        else {
          $build['#markup'] = '';
        }
      }
      $element[$delta] = $build;
    }
  }
  if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
    $linked = $display['settings']['taxonomy_term_link'];
    $terms = array();
    foreach ($items as $delta => $item) {
      if ($item['tid'] == 'autocreate') {

        // We don't necessarily have a link when this is
        // an autocreated term, usually in preview.
        // So just send the term as check plained markup.
        $item_display = check_plain($item['name']);
      }
      else {
        $term = taxonomy_term_load($item['tid']);
        if (module_exists('i18n_taxonomy')) {
          $term = i18n_taxonomy_localize_terms($term);
        }
        if (!is_object($term)) {

          // This term is missing in the database.
          continue;
        }
        if ($display['type'] == 'ds_taxonomy_separator_localized' && function_exists('i18n_taxonomy_term_name')) {
          $term->name = i18n_taxonomy_term_name($term, $language->language);
        }
        $item_display = check_plain($term->name);
        if ($linked) {
          $uri = entity_uri('taxonomy_term', $term);
          $item_display = l($term->name, $uri['path'], $uri['options']);
        }
      }
      $terms[] = $item_display;
    }
    if (!empty($terms)) {
      $output = '';
      $separator = $display['settings']['taxonomy_term_separator'];
      $output = implode($separator, $terms);
      $element[0] = array(
        '#markup' => $output,
      );
    }
  }
  return $element;
}