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

Comments

longwave created an issue. See original summary.

prabuela’s picture

Assigned: Unassigned » prabuela
prabuela’s picture

Assigned: prabuela » Unassigned
prabuela’s picture

Assigned: Unassigned » prabuela
prabuela’s picture

StatusFileSize
new773 bytes
prabuela’s picture

Assigned: prabuela » Unassigned
Status: Active » Needs review
longwave’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
@@ -94,6 +94,10 @@ public function load() {
+  protected function getQuery() {

Thanks, this method needs some documentation but this looks good otherwise.

prabuela’s picture

Assigned: Unassigned » prabuela
StatusFileSize
new1018 bytes
prabuela’s picture

Assigned: prabuela » Unassigned
Status: Needs work » Needs review
joachim’s picture

Status: Needs review » Needs work

Good idea, just a few things to fix:

  1. +++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
    @@ -91,9 +91,19 @@ public function load() {
    +   *   Execute the array of entity IDs.
    

    That doesn't make sense as a @return. Does this need changing?

  2. +++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
    @@ -91,9 +91,19 @@ public function load() {
    +   * Loads entity IDs using the entity id.
    +   *
    +   * @return array
    +   *   An array of entity IDs.
    

    That's wrong.

    Looks like the docblocks have got swapped around maybe?

  3. +++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
    @@ -91,9 +91,19 @@ public function load() {
    +  protected function getQuery() {
    

    As this is a new method, and protected, it should have a return type.

prabuela’s picture

StatusFileSize
new1.02 KB
prabuela’s picture

StatusFileSize
new1.02 KB
prabuela’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Seems the points from #10 have not been addressed

Also please include an interdiff with patches

Patch file names also seem incorrect.

akram khan’s picture

StatusFileSize
new1.29 KB
new1.13 KB

added updated patch and address comment #10

prabuela’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Still doesn't seem correct

+ protected function getQuery(): QueryInterface {

But the doc is saying an array is returned.

akram khan’s picture

StatusFileSize
new1.32 KB
new585 bytes

made changes according to comment #17

akram khan’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Still doesn't seem correct.

Someone should check the code vs doing what's just written here.

But this reading now as

getQuery returns query object used to load entity IDs.

But description says Loads entity IDs using the entity id.

akram khan’s picture

Status: Needs work » Needs review
StatusFileSize
new1.34 KB
new480 bytes

made changes now updated comment is more accurate and clarifies that the getQuery() method returns a query object for loading entity IDs from the storage

akram khan’s picture

StatusFileSize
new1.34 KB
new506 bytes

fixed CCF #21

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Think it's ready for committer review.

  • longwave committed 46853e85 on 10.1.x
    Issue #3332872 by Akram Khan, PrabuEla, smustgrave, longwave, joachim:...
longwave’s picture

Status: Reviewed & tested by the community » Fixed
+++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php
@@ -91,9 +92,19 @@ public function load() {
-   *   An array of entity IDs.
+   *   An array of entities.

It's still an array of entity IDs, the return type here hasn't changed. I fixed this on commit.

Committed and pushed 46853e85a1 to 10.1.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.