You are here

function date_handler_field_multiple::pre_render in Date 7

Same name and namespace in other branches
  1. 6.2 date/date_handler_field_multiple.inc \date_handler_field_multiple::pre_render()

Run before any fields are rendered.

This gives the handlers some time to set up before any handler has been rendered.

Parameters

array $values: An array of all objects returned from the query.

Overrides views_handler_field::pre_render

File

date_views/includes/date_handler_field_multiple.inc, line 55
An extended subclass for field handling that adds multiple field grouping.

Class

date_handler_field_multiple
@file An extended subclass for field handling that adds multiple field grouping.

Code

function pre_render($values) {

  // If there are no values to render (displaying a summary, or query returned no results),
  // or if this is not a grouped field, do nothing specific.
  if (isset($this->view->build_info['summary']) || empty($values) || !$this->defer_query) {
    return parent::pre_render($values);
  }
  $field_name = $this->content_field_name;
  $field = field_info_field($field_name);
  $db_info = date_api_database_info($field);
  $options = $this->options;
  $this->view->date_info->date_handler_fields = date_handler_fields($this->view);

  // Build the list of vids to retrieve.
  // TODO: try fetching from cache_content first ??
  $vids = array();
  $this->field_values = array();
  foreach ($values as $result) {
    if (isset($result->{$this->field_alias})) {
      $vids[] = $result->{$this->field_alias};
    }
  }

  // List columns to retrieve.
  $alias = content_views_tablename($field);

  // Prefix aliases with '_' to avoid clashing with field columns names.
  $query_columns = array(
    'node.vid AS _vid',
    "{$alias}.delta as _delta",
    // nid is needed to generate the links for 'link to node' option.
    'node.nid AS _nid',
  );

  // The actual field columns.
  foreach ($db_info['columns'] as $column => $attributes) {
    $query_columns[] = "{$alias}.{$attributes['column']} AS {$column}";
    $query_fields[] = "{$alias}.{$attributes['column']}";
  }

  // Retrieve all values, we limit them in date_prepare_node(),
  // a function that is used both by the handler and by the
  // node theme to take advantage of formatter settings.
  $where = array(
    '1',
  );
  $query = 'SELECT ' . implode(', ', $query_columns) . ' FROM {' . $db_info['table'] . "} {$alias}" . " LEFT JOIN {node} node ON node.vid = {$alias}.vid" . " WHERE node.vid IN (" . implode(',', $vids) . ') AND ' . implode(' OR ', $where) . " ORDER BY node.nid ASC, {$alias}.delta ASC";
  $result = db_query($query);
  while ($item = db_fetch_array($result)) {

    // Clean up the $item from vid and delta. We keep nid for now.
    $vid = $item['_vid'];
    unset($item['_vid']);
    $delta = !empty($item['_delta']) ? $item['_delta'] : 0;
    $item['#delta'] = $item['_delta'];
    unset($item['_delta']);
    $this->field_values[$vid][$delta] = $item;
  }
}