Allow EntityListBuilder::getEntityIds() to easily extend the query

Created on 12 January 2023, almost 2 years ago
Updated 30 April 2023, over 1 year ago

Problem/Motivation

I have a case where I want to add a condition to the query in an EntityListBuilder. To do this I have to copy and override the entire getEntityIds() method:

  /**
   * Loads entity IDs using a pager sorted by the entity id.
   *
   * @return array
   *   An array of entity IDs.
   */
  protected function getEntityIds() {
    $query = $this->getStorage()->getQuery()
      ->accessCheck(TRUE)
      ->sort($this->entityType->getKey('id'));

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query->execute();
  }

By extracting the query to its own method, this would be easier to extend.

Steps to reproduce

Proposed resolution

Add a getQuery() method:

  protected function getEntityIds() {
    return $this->getQuery()->execute();
  }

  protected function getQuery() {
    $query = $this->getStorage()->getQuery()
      ->accessCheck(TRUE)
      ->sort($this->entityType->getKey('id'));

    // Only add the pager if a limit is specified.
    if ($this->limit) {
      $query->pager($this->limit);
    }
    return $query;
  }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Fixed

Version

10.1

Component
Entity 

Last updated about 10 hours ago

Created by

🇬🇧United Kingdom longwave UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024