diff --git a/core/modules/field/tests/src/Kernel/EntityReference/Views/ViewsSelectionWithLimitedOutputTest.php b/core/modules/field/tests/src/Kernel/EntityReference/Views/ViewsSelectionWithLimitedOutputTest.php new file mode 100644 index 0000000000..1f4c428533 --- /dev/null +++ b/core/modules/field/tests/src/Kernel/EntityReference/Views/ViewsSelectionWithLimitedOutputTest.php @@ -0,0 +1,91 @@ +installEntitySchema('user'); + $this->installEntitySchema('node'); + $this->installConfig('entity_reference_test'); + + // Create 20 test nodes, all containing a known string sequence. + NodeType::create(['type' => 'some_kind_of_node'])->save(); + for ($i = 0; $i < 20; $i++) { + Node::create([ + 'type' => 'some_kind_of_node', + 'title' => $this->randomString() . ' string to search for', + 'status' => NodeInterface::PUBLISHED, + ])->save(); + } + + $this->view = Views::getView('test_entity_reference'); + $this->view->initDisplay(); + + // Allow the test to access the view. + $this->view->displayHandlers->get('entity_reference_1')->overrideOption('access', ['type' => 'none']); + $this->view->save(); + } + + /** + * Tests that the Views selection limits the number of results correctly. + */ + public function testLimitedOutput() { + /** @var \Drupal\Core\Entity\EntityAutocompleteMatcher $matcher */ + $matcher = $this->container->get('entity.autocomplete_matcher'); + $selection_settings = [ + 'view' => [ + 'view_name' => 'test_entity_reference', + 'display_name' => 'entity_reference_1', + 'arguments' => [], + ], + ]; + + // Check that 10 items are returned when the view has no pager. + $result = $matcher->getMatches('node', 'views', $selection_settings, 'string to search for'); + $this->assertCount(10, $result); + + // Add a pager to the view different than 10. + $pager = ['type' => 'some', 'options' => ['items_per_page' => 15]]; + $this->view->displayHandlers->get('entity_reference_1')->overrideOption('pager', $pager); + + // Check that the returned number of items equals the view pager length. + $result = $matcher->getMatches('node', 'views', $selection_settings, 'string to search for'); + $this->assertCount(15, $result); + } + +} diff --git a/core/modules/views/src/Plugin/views/display/EntityReference.php b/core/modules/views/src/Plugin/views/display/EntityReference.php index 4dedd51a83..30eb62050b 100644 --- a/core/modules/views/src/Plugin/views/display/EntityReference.php +++ b/core/modules/views/src/Plugin/views/display/EntityReference.php @@ -156,6 +156,14 @@ public function query() { $this->view->query->addWhere(0, $id_table . '.' . $id_field, $options['ids'], 'IN'); } + // 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()); + } $this->view->setItemsPerPage($options['limit']); }