Inside a block plugin I'm creating a table (in Drupal8). Like so
$header = [
t('Name'),
t('Number'),
t('Rank'),
];
$nids = \Drupal::entityQuery('node')
->extend('Drupal\Core\Database\Query\TableSortExtender')
->orderByHeader($header)
->condition('type', 'my_content_type')
->condition('status', 1)
->pager(5)
->execute();
foreach ($nids as $nid) {
$node = \Drupal\node\Entity\NODE::load($nid);
$row = [
'data' => [
$node->title->value,
$node->field_number->value,
$node->field_rank->value,
],
];
$rows[] = $row;
}
$render = [
'table' => [
'#prefix' => '<h1>Entries</h1>',
'#theme' => 'table',
'#attributes' => [
'data-striping' => 0
],
'#header' => $header,
'#rows' => $rows,
],
];
return $render;
Without the code below
->extend('Drupal\Core\Database\Query\TableSortExtender')
->orderByHeader($header)
the table renders just fine, but if I do include it I get the error
Call to undefined method Drupal\Core\Entity\Query\Sql\Query::extend()
I'm guessing that it ->extend does not work on EntityQueries, but if that's the case how can I add table sorting to my table?
UPDATE: I've changed my $header array to include 'field' like so
$header = [
[
'data' => t('Title'),
'field' => 'title'
],
[
'data' => t('Race'),
'field' => 'field_number'
],
[
'data' => t('Time'),
'field' => 'field_rank',
],
];
and tried adding
->tableSort($header)
to the query and removing extend and orderByHeader from the query but then I receive the following error
Drupal\Core\Entity\Query\QueryException: '' not found in Drupal\Core\Entity\Query\Sql\Tables->ensureEntityTable()
UPDATE 2: I tried changing the $header array to use a specifier and type property like so.
'data' => t('Name'),
'type' => 'field',
'specifier' => [
'field' => 'title',
'column' => 'value',
],
But I end up with the same error and get an additional error
Undefined index: specifier in Drupal\Core\Entity\Query\QueryBase->tableSort()