Problem/Motivation

Hello,

The limit of entity reference when I use views is not working it's aways 10 instead of use the views pager

Steps to reproduce

  • Create a new views and create a display type entity reference
  • Select 50 items on pager
  • Go to any content type and create an entity reference field
  • On reference method select views after that select your view
  • Create a new content type, fill out anything that have more than 10 contents on reference field, it will show just 10 items

Proposed resolution

If a limit is set on the view via a pager, use that setting. If no pager is set on the view, add a pager with a limit of 10 so users don't unexpectedly dig themselves into a huge performance hit when removing the pager from the view.

Remaining tasks

Add patch to use the pager
Add tests

User interface changes

None, UI settings are now actually used instead of ignored.

API changes

none

Data model changes

none

Issue fork drupal-2772523

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

guilopes created an issue. See original summary.

guilopes’s picture

Status: Needs work » Needs review
FileSize
775 bytes
guilopes’s picture

I forgot one thing on the last patch :)

guilopes’s picture

Issue summary: View changes
guilopes’s picture

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

guilopes’s picture

any updates on it?

claudiu.cristea’s picture

Status: Needs review » Closed (works as designed)

I don't think this is a good idea. The limit is hardcoded to 10 in \Drupal\Core\Entity\EntityAutocompleteMatcher::getMatches(), line:

      $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10);

This, I think, was the intention, to limit the matches to a decent number and preventing the flood of Ajax response with a too large list.

guilopes’s picture

I know, but it don't make sense when we have a view. If I want to show more than 10 I need to hack the core to do that?

I really want more than 10 items on my project because only 10 is not enought for my use case and for me it's the expected behavior when I have a view. What do you guys think about that?

guilopes’s picture

Status: Closed (works as designed) » Needs review
dawehner’s picture

Issue tags: +Needs tests

No matter what we do, we should ensure that some test coverage is given.

Fidelix’s picture

Sorry but it doesn't make sense to hardcode this value to 10.
People may have different requirements and it's such a simple thing to change.

Test coverage however is certainly necessary.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jonathanshaw’s picture

Re #8 can we make the limit 10, unless an explicit limit is set in the view (in which case use that)? This way it works out of the box no matter how many items in the view, but you can get more than 10 if you explicitly say you want that.

Entity reference views are single-purpose by their nature, so they are crafted to serve this autocomplete, and we can place a high level of responsibility on sitebuilders to configure them right - it's not like autocomplete is using a views display that is also used for some other purpose that might require a different limit.

Lendude’s picture

Status: Needs review » Needs work

I agree that if we give the option to set this, we should use the setting. So either we remove the ability to set the number of items for entity references or we actually use the setting. I would be for using the setting.

I agree with @claudiu.cristea that this might lead to issues when set to a high number, but there are many ways to use Views to dig yourself into a huge performance hole if you don't know what you are doing, so *shrug*

Needs work for tests.

mikeker’s picture

Status: Needs work » Postponed
FileSize
2.07 KB

I think we should postpone this issue until #2174633: View output is not used for entityreference options is resolved. Once that fix is in, here is a test to validate this fix (uploading as txt as it won't apply until 2174633 is committed).

mikeker’s picture

+++ b/core/modules/field/tests/src/Functional/EntityReference/Views/SelectionTest.php
@@ -82,6 +82,41 @@ protected function setUp() {
+   * @group thisone

Ooops. Left my debug code in there. This should be removed when we reactivate this issue.

guilopes’s picture

Status: Postponed » Patch (to be ported)

@mikeker I don't think we need to wait that issue, since it's just the number of items. It's a really simple feature and I think it should be in core ASAP

claudiu.cristea’s picture

Status: Patch (to be ported) » Needs review
Issue tags: -Needs tests
FileSize
3 KB
4.03 KB
4.82 KB

OK, let's limit to view's display limit if one is set.

Few notes:

  • The test (provided in #16) is BrowserTestBase but we can achieve the same with a KernelTestBase.
  • Writing the test, I discovered also a bug. When a view with no pager (pager plugin ID is none) is used, all the records are returned. But this is not OK as we need, at least, to fallback to the hard-limit of 10. This bug can be observed in the "test only" patch when the view is used without a pager. Normally, users are not noticing this bug because the entity reference display comes with pager by default. We should enforce a pager in such cases and use the hard limit of 10 items.

The last submitted patch, 19: 2772523-19-test-only.patch, failed testing.

mikeker’s picture

I don't think we need to wait that issue, since it's just the number of items.

I agree. I originally thought that fixing the above mentioned issue would include the limits imposed by the view but I was wrong. The code changes included in #19 (and earlier patches) are needed. Thank you for correcting that, @guilopes!

+++ b/core/modules/views/src/Plugin/views/display/EntityReference.php
@@ -156,6 +156,14 @@ public function query() {
+    // If the display has no pager set (plugin ID equals 'none'), set one.
+    if ($this->view->getPager()->getPluginId() === 'none') {
+      /** @var \Drupal\views\Plugin\ViewsPluginManager $pager_plugin_manager */
+      $pager_plugin_manager = \Drupal::service('plugin.manager.views.pager');
+      /** @var \Drupal\views\Plugin\views\pager\PagerPluginBase $pager */
+      $this->view->pager = $pager_plugin_manager->createInstance('some');
+      $this->view->pager->init($this->view, $this->view->getDisplay());
+    }

I disagree with this. I don't think we should be setting pagers when a site builder has specifically removed it (a default view comes with a pager of 10 items). I can't think of a good reason to allow unlimited options in an autocomplete, but that doesn't mean the reason doesn't exist.

We should be providing reasonable defaults and minimal limitations for site builders.

mikeker’s picture

Status: Needs review » Needs work

The last submitted patch, 22: 2772523-22-entity-reference-limit.patch, failed testing.

claudiu.cristea’s picture

I totally disagree with not keeping, at least, the 10 items fallback. The rule that I'm proposing is: If a pager has been explicitly provided by the view, use it as limit. Otherwise default to 10 items. I would not allow infinite (the max count returned by the view) items -- there's a very good reason for limiting the autocomplete suggestion list.

jonathanshaw’s picture

I don't see that it matters in practice. It'd be a shame to block the patch on this point.

But because we need to decide one way or the other, I suggest we have the 10 item fallback limit if no pager as suggested by @claudiu.cristea, because a sitebuilder can always specify a pager with a 100000 item limit if that really really is what they want. In other words, if they want to live dangerously then they have to be explicit about it.

I can see more easily imagine situations in which a sitebuilder could get into trouble if there was no fallback limit, than I can situations in which they can't achieve what they want because there is a fallback limit.

mikeker’s picture

Status: Needs work » Needs review
FileSize
4.8 KB
1.83 KB

@claudiu.cristea, @jonathanshaw: Good arguments and I concede the point. As @jonathanshaw points out a site builder could set the limit to 9999999 if they really wanted all results. Still, I'm worried about adding a pager to a view that doesn't have one, but I can quash that worry in order to get this into core... :)

The attached patch is revert of #22, which means is should be identical to @claudiu.cristea's patch in #19.

Lendude’s picture

++ to adding the pager when none is set. People are free to set the limit on a ridiculous number, but at least they would do so knowingly. I think the solution to add a pager is more elegant then just hardcoding a limit on 10, if none is provided. With the hardcoded limit a site-builder would then have no idea where the limit is coming from and would be completely in the dark about how to circumvent this.

Updated the IS to show the current fix direction.

Not real happy with the spot where we are fixing this. Any call to \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::initializeView will now get the limit set to the View limit.

So this call in \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::validateReferenceableEntities

    if ($this->initializeView(NULL, 'CONTAINS', 0, $ids)) {
      // Get the results.
      $entities = $this->view->executeDisplay($display_name, $arguments);
      $result = array_keys($entities);
    }

which hardcodes its limit to 0, will now not use the passed 0, but the set limit on the View.

Moving the logic to \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::getReferenceableEntities makes more sense to me, but that is also passed a $limit so the same would still go, just on a higher level (and it leads to some code duplication because we need to verify the View display twice before using it). So open to other ideas if somebody has them.

Added some dependency injection too.

guilopes’s picture

Can we apply this now ?

guilopes’s picture

Status: Needs review » Reviewed & tested by the community
guilopes’s picture

Any answer here? I think we should merge it

tstoeckler’s picture

Status: Reviewed & tested by the community » Needs work

I think it's pretty strange, though, that we are now completely throwing away the passed-in $limit as soon as the view has a pager. I think we should either check if the passed limit is 0 or maybe even do something like

min($passed_limit,
 $views_limit)

, but just (conditionally) ignoring it is strange IMO.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Publishing Future’s picture

Is there an update of the patch in #27 for Drupal 8.6 available?

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

colan’s picture

#31: !empty() should ensure it's > 0. That should be sufficient.

swatichouhan012’s picture

Assigned: Unassigned » swatichouhan012

I am working on this..

swatichouhan012’s picture

Assigned: swatichouhan012 » Unassigned
colan’s picture

@swatichouhan012: Did you accidentally unassign yourself, or were you only planning on working on it for 2 hours & 14 minutes? :)

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

steveoriol’s picture

This issue is still present in D9.3.0...

cilefen’s picture

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kopeboy’s picture

It's a shame this isn't fixed yet after years in core.
Any site builder CANNOT force an entity reference selection: even if his custom logic defined in a View results in 1 item, a checkbox on the entity form will list multiple options, that he cannot even see in the Views' preview!

Also, why can't this be fixed in 10.1.x instead of 11?!

demon326’s picture

I ran into this small issue using webforms to make a more visual selection of nodes. The patches are not working for 10.2.x. Would be great if this could be fixed , because showing ALL the results on a single page will result in performance issues.

kopeboy’s picture

I was able to fix this just by adding to
/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php

(ie. simpler than patch#27)

After the line with $this->view->setDisplay($display_name);
and before the line with $entity_reference_options = [
add these lines:

    $this->view->initPager();
    $items_per_page = $this->view->getItemsPerPage();
    if ($items_per_page > 0) {
      $limit = $items_per_page;
    }
Anchal_gupta’s picture

FileSize
829 bytes

I have uploaded the patch
Address #53 comment
Please review

demon326’s picture

#53 and patch from #54 seem to be working

edit: Patch work, but not page navigation is possible.

kopeboy’s picture

Status: Needs work » Reviewed & tested by the community

What do you mean?
There is no paging in entity references and if you set a limit in the View with "Display a specified number of items", you are explicitly removing the pager.
I just tested that the pager on another display (of type page) of the same View works.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work

The Needs Review Queue Bot tested this issue.

While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)

Prem Suthar made their first commit to this issue’s fork.

Prem Suthar’s picture

Status: Needs work » Needs review

Create the Mr For Issue. Please Review

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This will need test coverage

#54 appears to have dropped a good portion of the original patch, how come? @Prem Suthar see you opened an MR for that too so maybe you know too?