1

I want to add a query to filter my entity list table.

class CommandesLignesEntityListBuilder extends EntityListBuilder {

  use LinkGeneratorTrait;

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['id'] = $this->t('ID');
    $header['produit'] = $this->t('Produit');
    $header['quantite'] = $this->t('Quantité');
    $header['prixHT'] = $this->t('Prix HT');
    $header['TVA'] = $this->t('TVA');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /* @var $entity \Drupal\boutique\Entity\CommandesLignesEntity */
    $row['id'] = $entity->id();
    $row['produit'] =$entity->getProduit();
    $row['quantite'] =$entity->getQTE();
    $row['prixHT'] =$entity->getPrixHT();
    $row['TVA'] =$entity->getTVA();

    return $row + parent::buildRow($entity);
  }

}

This show me all lines. How can I filter this with a query? For example, how can I get only lines with price greater or equal to 100?

i would like a permanent query for this EntityListBuilder sub-class.

2 Answers 2

5

If someone is looking for the same, this is how i use filter on my entityListBuilder :

I override the method getEntityIds for build my logic :

   protected function getEntityIds() {
        $query = $this->getStorage()->getQuery()
            ->sort($this->entityType->getKey('id'))
            ->condition('commandes_id',1,'=');

        if ($this->limit) {
            $query->pager($this->limit);
        }
        return $query->execute();
    }




  /**
   * {@inheritdoc}
   */
  public function buildHeader() {

    $header = array(
        'id' => array(
            'data' => $this->t('N° produit'),
            'class' => array(RESPONSIVE_PRIORITY_LOW),
        ),
        'nom' => array(
            'data' => $this->t('Nom'),
        ),

        'quantite' => array(
            'data' => $this->t('Quantité'),
        ),

        'prixHT' => array(
            'data' => $this->t('Prix HT'),
        ),

        'tva' => array(
            'data' => $this->t('TVA'),
        ),

    );

    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /* @var $entity \Drupal\boutique\Entity\CommandesLignesEntity */
    $row['id'] = $entity->id();
    $row['produit'] =$entity->getProduit();
    $row['quantite'] =$entity->getQTE();
    $row['prixHT'] =$entity->getPrixHT();
    $row['TVA'] =$entity->getTVA();

    return $row + parent::buildRow($entity);
  }
1
  • 1
    I +1 this as an alternative, but just to clarify, the load method calls getEntityIds by default, so the query should probably be modified in getEntityIds rather than in load. That way someone might call getEntityIds and get completely different results.
    – mradcliffe
    Commented May 16, 2016 at 19:41
4

Although not exactly a duplicate of How to make EntityListBuilder sortable? it is essentially the same answer as Berdir wrote,

You'd have to implement the query logic yourself.

For instance, BlockListBuilder overrides getEntityIds to add a condition on the theme key.

However as Berdir also notes,

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.

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.