1 date.module date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display)

Helper function to adapt entity date fields to formatter settings.

File

core/modules/date/date.module, line 474
Defines date/time field types.

Code

function date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
  // If there are options to limit multiple values,
  // alter the entity values to match.
  $field_name = $field['field_name'];
  $options = $display['settings'];
  $max_count = $options['multiple_number'];

  // If no results should be shown, empty the values and return.
  if (is_numeric($max_count) && $max_count == 0) {
    $entity->{$field_name} = array();
    return $entity;
  }

  // Otherwise removed values that should not be displayed.
  if (!empty($options['multiple_from']) || !empty($options['multiple_to']) || !empty($max_count)) {
    $format = date_type_format($field['type']);
    $date_handler = new date_sql_handler($field);
    $arg0 = !empty($options['multiple_from']) ? $date_handler->arg_replace($options['multiple_from']) : DATE_MIN_YEAR . '-01-01T00:00:00';
    $arg1 = !empty($options['multiple_to']) ? $date_handler->arg_replace($options['multiple_to']) : DATE_MAX_YEAR . '-12-31T23:59:59';
    if (!empty($arg0) && !empty($arg1)) {
      $arg = $arg0 . '--' . $arg1;
    }
    elseif (!empty($arg0)) {
      $arg = $arg0;
    }
    elseif (!empty($arg1)) {
      $arg = $arg1;
    }
    if (!empty($arg)) {
      $range = $date_handler->arg_range($arg);
      $start = date_format($range[0], $format);
      $end = date_format($range[1], $format);
      // Empty out values we don't want to see.
      $count = 0;
      foreach ($entity->{$field_name}[$langcode] as $delta => $value) {
        if (!empty($entity->date_repeat_show_all)) {
          break;
        }
        elseif ((!empty($max_count) && is_numeric($max_count) && $count >= $max_count) || 
          (!empty($value['value']) && $value['value'] < $start) || 
          (!empty($value['value2']) && $value['value2'] > $end)) {
          unset($entity->{$field_name}[$langcode][$delta]);
        }
        else {
          $count++;
        }
      }
    }
  }

  return $entity;
}