1

I have an EntityFieldQuery that gets displayed in a table. How do I add clickable sort-by-column functionality? When I use regular db_select queries I can sort using the following code:

 $query = db_select('tablename', 't')
   ->extend('PagerDefault')
   ->limit(50)
   ->extend('TableSort')

 // ...

 $query = $query->orderByHeader($header)
 $results = $query->execute();

Everything looks good and when the table is displayed I can click on the columns and they are hyperlinks and I can sort just fine. However, now that I have an EntityFieldQuery I can't seem to duplicate the functionality. This is what I have so far:

$header = array(
  'title' => array(
    'data' => t('Date'),
    'type' => 'field',
    'specifier' => array(
      'field' => 'field_articledate',
      'column' => 'value',
    ),
  ),
);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'article')
  ->propertyCondition('status', 1);

$query = $query->tableSort($header);
$query = $query->pager(100);

$results = $query->execute();

A one-column header appears and it says "Date" as expected, but it is not a clickable hyperlink. What am I doing wrong? What is the correct syntax?

3
  • Maybe it's getting confused between the array key (title), which is a node property, and the field specifier you've given. To test just try changing the array key to something else
    – Clive
    Feb 27, 2015 at 18:13
  • I changed 'title' to 'something' and it did not fix the problem.
    – MrSnrub
    Feb 27, 2015 at 19:13
  • @MrSnrub : Did you find this solution. If yes then post it. i m also facing the same issue. Jul 22, 2015 at 9:34

1 Answer 1

2

I know this thread is a little old but adding 'field' to the array fixes the issue.

e.g.

$header = array(
  'title' => array(
    'data' => t('Date'),
    'type' => 'field',
    'specifier' => array(
      'field' => 'field_articledate',
      'column' => 'value',
    ),
    'field' => '', //There is an Isset call that prevents links from showing without this.
  ),
);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'article')
  ->propertyCondition('status', 1);

$query = $query->tableSort($header);
$query = $query->pager(100);

$results = $query->execute();

See: https://www.drupal.org/node/885014#comment-3464666 for a comment on the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.