7.x entity.inc EntityFieldQuery::tableSort(&$headers)

Enables sortable tables for this query.

Parameters

$headers: An EFQ Header array based on which the order clause is added to the query.

Return value

EntityFieldQuery The called object.

File

includes/entity.inc, line 1069

Class

EntityFieldQuery
Retrieves entities matching a given set of conditions.

Code

public function tableSort(&$headers) {

  // If 'field' is not initialized, the header columns aren't clickable
  foreach ($headers as $key => $header) {
    if (is_array($header) && isset($header['specifier'])) {
      $headers[$key]['field'] = '';
    }
  }
  $order = tablesort_get_order($headers);
  $direction = tablesort_get_sort($headers);
  foreach ($headers as $header) {
    if (is_array($header) && $header['data'] == $order['name']) {
      if ($header['type'] == 'field') {
        $this
          ->fieldOrderBy($header['specifier']['field'], $header['specifier']['column'], $direction);
      }
      else {
        $header['direction'] = $direction;
        $this->order[] = $header;
      }
    }
  }
  return $this;
}

Comments

FFFFFFFab’s picture

I'm trying to use this method but I don't know what datas $headers must have. It's seems that $headers doesn't look like $headers used in theme_table.
What means "An EFQ Header" ?

Thanks,

Fab.

ralphb’s picture

Here's an example I extracted from modules/simpletest/tests/entity_query.test:

    $headers = array(
      'myproperty' => array(
        'data' => 'TableColumnName1',
        'type' => 'property',
        'specifier' => 'title',
      ),
      'myfield1' => array(
        'data' => 'TableColumnName2',
        'type' => 'field',
        'specifier' => array('field' => 'field_my_field1', 'column' => 'value'),
      ),
      'myfield2' => array(
        'data' => 'TableColumnName3',
        'type' => 'field',
        'specifier' => array('field' => 'field_my_field2', 'column' => 'value'),
        'sort' => 'desc', # sort by this column by default
      ),
    );
Rob230’s picture

Note that if you order by a field then rows without a value for that field get removed. See http://drupal.org/node/1611438