You are here

function date_prepare_entity in Date 7.3

Same name and namespace in other branches
  1. 8 date.module \date_prepare_entity()
  2. 7 date.module \date_prepare_entity()
  3. 7.2 date.module \date_prepare_entity()

Helper function to adapt entity date fields to formatter settings.

1 call to date_prepare_entity()
theme_date_display_combination in ./date.theme
Returns HTML for a date element formatted as a Start/End combination.

File

./date.module, line 538

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']);
    include_once drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
    $date_handler = new date_sql_handler($field);
    $arg0 = !empty($options['multiple_from']) ? $date_handler
      ->arg_replace($options['multiple_from']) : variable_get('date_min_year', 100) . '-01-01T00:00:00';
    $arg1 = !empty($options['multiple_to']) ? $date_handler
      ->arg_replace($options['multiple_to']) : variable_get('date_max_year', 4000) . '-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;
}