You are here

function date_field_formatter_view in Date 7.2

Same name and namespace in other branches
  1. 7.3 date.field.inc \date_field_formatter_view()
  2. 7 date.field.inc \date_field_formatter_view()

Implements hook_field_formatter_view().

File

./date.field.inc, line 101
Field hooks to implement a date field.

Code

function date_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  // Useful values:
  // - $entity->date_id
  //   If set, this will show only an individual date on a field with
  //   multiple dates. The value should be a string that contains
  //   the following values, separated with periods:
  //   - module name of the module adding the item
  //   - node nid
  //   - field name
  //   - delta value of the field to be displayed
  //   - other information the module's custom theme might need
  //   Used by the calendar module and available for other uses.
  //   example: 'date:217:field_date:3:test'
  // - $entity->date_repeat_show
  //   If TRUE, tells the theme to show all the computed values
  //   of a repeating date. If not TRUE or not set, only the
  //   start date and the repeat rule will be displayed.
  $element = array();
  $settings = $display['settings'];
  $formatter = $display['type'];
  $variables = array(
    'entity' => $entity,
    'entity_type' => $entity_type,
    'field' => $field,
    'instance' => $instance,
    'langcode' => $langcode,
    'items' => $items,
    'display' => $display,
    'dates' => array(),
    'attributes' => array(),
    'rdf_mapping' => array(),
    'add_rdf' => module_exists('rdf'),
    'microdata' => array(),
    'add_microdata' => module_exists('microdata'),
  );

  // If the microdata module is enabled, the microdata mapping will have been
  // passed in via the entity.
  if ($variables['add_microdata'] && isset($entity->microdata[$field['field_name']])) {
    $variables['microdata'] = $entity->microdata[$field['field_name']];
  }

  // If there is an RDf mapping for this date field, pass it down to the theme.
  $rdf_mapping = array();
  if (!empty($entity->rdf_mapping) && function_exists('rdf_rdfa_attributes')) {
    if (!empty($entity->rdf_mapping[$field['field_name']])) {
      $variables['rdf_mapping'] = $rdf_mapping = $entity->rdf_mapping[$field['field_name']];
    }
  }

  // Trigger hook_date_formatter_pre_view_alter().
  // Give other modules a chance to prepare the entity before formatting it.
  drupal_alter('date_formatter_pre_view', $entity, $variables);

  // See if we are only supposed to display a selected
  // item from multiple value date fields.
  $selected_deltas = array();
  if (!empty($entity->date_id)) {
    foreach ((array) $entity->date_id as $key => $id) {
      list($module, $nid, $field_name, $selected_delta, $other) = explode('.', $id . '.');
      if ($field_name == $field['field_name']) {
        $selected_deltas[] = $selected_delta;
      }
    }
  }
  switch ($display['type']) {
    case 'date_plain':
      foreach ($items as $delta => $item) {
        if (!empty($entity->date_id) && !in_array($delta, $selected_deltas)) {
          continue;
        }
        else {
          if (empty($item['value2']) || $item['value'] == $item['value2']) {
            $element[$delta] = array(
              '#markup' => $item['value'],
            );
          }
          else {
            $element[$delta] = array(
              '#markup' => t('!start-date to !end-date', array(
                '!start-date' => $item['value'],
                '!end-date' => $item['value2'],
              )),
            );
          }
        }
      }
      break;
    case 'format_interval':
      foreach ($items as $delta => $item) {
        if (!empty($entity->date_id) && !in_array($delta, $selected_deltas)) {
          continue;
        }
        else {
          $variables['delta'] = $delta;
          $variables['item'] = $item;
          $variables['dates'] = date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
          $variables['attributes'] = !empty($rdf_mapping) ? rdf_rdfa_attributes($rdf_mapping, $item['value']) : array();
          $element[$delta] = array(
            '#markup' => theme('date_display_interval', $variables),
          );
        }
      }
      break;
    default:
      foreach ($items as $delta => $item) {
        if (!empty($entity->date_id) && !in_array($delta, $selected_deltas)) {
          continue;
        }
        else {
          $variables['delta'] = $delta;
          $variables['item'] = $item;
          $variables['dates'] = date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
          $variables['attributes'] = !empty($rdf_mapping) ? rdf_rdfa_attributes($rdf_mapping, $item['value']) : array();
          $variables['show_remaining_days'] = isset($display['settings']['show_remaining_days']) ? $display['settings']['show_remaining_days'] : FALSE;
          $output = theme('date_display_combination', $variables);
          if (!empty($output)) {
            $element[$delta] = array(
              '#markup' => $output,
            );
          }
        }
      }
  }

  // Add the CSS stylesheet only if we have something to display.
  if (!empty($element)) {
    $element['#attached']['css'][] = drupal_get_path('module', 'date_api') . '/date.css';
  }
  return $element;
}