Drupal - How to make EntityListBuilder sortable?
This is how i sort my entityListBuilder :
i use the method load() :
public function load() {
$entity_query = \Drupal::service('entity.query')->get('produits_entity');
$header = $this->buildHeader();
$entity_query->pager(50);
$entity_query->tableSort($header);
$uids = $entity_query->execute();
return $this->storage->loadMultiple($uids);
}
Then i build my header like this :
/**
* {@inheritdoc}
*/
public function buildHeader() {
$header = array(
'id' => array(
'data' => $this->t('N° produit'),
'field' => 'id',
'specifier' => 'id',
'class' => array(RESPONSIVE_PRIORITY_LOW),
),
'nom' => array(
'data' => $this->t('Nom'),
'field' => 'name',
'specifier' => 'name',
),
'prixHT' => array(
'data' => $this->t('Prix HT'),
'field' => 'prixHT',
'specifier' => 'prixHT',
),
'statut' => array(
'data' => $this->t('Actif'),
'field' => 'actif',
'specifier' => 'actif',
),
);
return $header + parent::buildHeader();
}
you need to add in your buildHeader() 2 param for each field you want sort :
name of field and specifier
Entity query doesn't have tablesort integration. You'd have to implement the query logic yourself.
For content entities, I would strongly recommend to rely on the views integration instead. List builders are limited in functionality and mostly useful for config entities only.