You are here

function date_prepare_entity in Date 8

Same name and namespace in other branches
  1. 7.3 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 487
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)) {
    $date_api_config = config('date_api.info');
    $format = date_type_format($field['type']);
    include_once drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
    $date_handler = new DateSqlHandler($field);
    $arg0 = !empty($options['multiple_from']) ? $date_handler
      ->arg_replace($options['multiple_from']) : $date_api_config
      ->get('year.min') . '-01-01T00:00:00';
    $arg1 = !empty($options['multiple_to']) ? $date_handler
      ->arg_replace($options['multiple_to']) : $date_api_config
      ->get('year.max') . '-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;
}