diff --git a/config/schema/linkit.schema.yml b/config/schema/linkit.schema.yml index 882a2f1cb84fdaeb2434ef20af2efc154ca06726..c6e3f1d10c38df8719df05c3339946cda6988c70 100644 --- a/config/schema/linkit.schema.yml +++ b/config/schema/linkit.schema.yml @@ -45,6 +45,8 @@ linkit.matcher.entity: type: boolean substitution_type: type: string + limit: + type: integer linkit.matcher.entity:*: type: linkit.matcher.entity diff --git a/src/Plugin/Linkit/Matcher/EntityMatcher.php b/src/Plugin/Linkit/Matcher/EntityMatcher.php index a8a85c8f3cf5178a85909f6bd7a837828c8afd32..4231b509afdf94ee9055c03ba2e72e57b73c3b3c 100644 --- a/src/Plugin/Linkit/Matcher/EntityMatcher.php +++ b/src/Plugin/Linkit/Matcher/EntityMatcher.php @@ -36,6 +36,11 @@ class EntityMatcher extends ConfigurableMatcherBase { use MatcherTokensTrait; + /** + * The default limit for matches. + */ + const DEFAULT_LIMIT = 100; + /** * The database connection. * @@ -161,6 +166,12 @@ class EntityMatcher extends ConfigurableMatcherBase { $summery[] = $this->t('Group by bundle: @bundle_grouping', [ '@bundle_grouping' => $this->configuration['group_by_bundle'] ? $this->t('Yes') : $this->t('No'), ]); + + if(!empty($configuration['limit'])){ + $summery[] = $this->t('Limit: @limit', [ + '@limit' => $this->configuration['limit'], + ]); + } } return $summery; @@ -175,6 +186,7 @@ class EntityMatcher extends ConfigurableMatcherBase { 'bundles' => [], 'group_by_bundle' => FALSE, 'substitution_type' => SubstitutionManagerInterface::DEFAULT_SUBSTITUTION, + 'limit' => static::DEFAULT_LIMIT, ] + parent::defaultConfiguration(); } @@ -257,6 +269,25 @@ class EntityMatcher extends ConfigurableMatcherBase { '#description' => $this->t('Configure how the selected entity should be transformed into a URL for insertion.'), ]; + $form['limit'] = [ + '#type' => 'details', + '#title' => $this->t('Limit'), + '#open' => TRUE, + ]; + + $form['limit']['limit'] = [ + '#type' => 'select', + '#options' => [ + 0 => $this->t('Unlimited'), + 20 => 20, + 50 => 50, + 100 => 100, + 200 => 200, + ], + '#title' => $this->t('Limit search results'), + '#description' => $this->t('Limit the amount of results displayed when searching.'), + '#default_value' => $this->configuration['limit'], + ]; return $form; } @@ -274,6 +305,7 @@ class EntityMatcher extends ConfigurableMatcherBase { $this->configuration['bundles'] = $form_state->getValue('bundles'); $this->configuration['group_by_bundle'] = $form_state->getValue('group_by_bundle'); $this->configuration['substitution_type'] = $form_state->getValue('substitution_type'); + $this->configuration['limit'] = $form_state->getValue('limit'); } /** @@ -353,6 +385,9 @@ class EntityMatcher extends ConfigurableMatcherBase { $query->condition($bundle_key, $this->configuration['bundles'], 'IN'); } + if($this->configuration['limit']){ + $query->range(0, $this->configuration['limit']); + } $this->addQueryTags($query); return $query; diff --git a/tests/src/Kernel/LinkitAutocompleteTest.php b/tests/src/Kernel/LinkitAutocompleteTest.php index 912d777d2a415b25cb92d597c8c77e571f00f5ed..e74d87ee7c0443d83754d158bb83bfe92ad3641f 100644 --- a/tests/src/Kernel/LinkitAutocompleteTest.php +++ b/tests/src/Kernel/LinkitAutocompleteTest.php @@ -152,6 +152,29 @@ class LinkitAutocompleteTest extends LinkitKernelTestBase { $this->assertEmpty(count($data), 'Autocomplete did not return any suggestions.'); } + /** + * Tests autocompletion with results limit. + */ + public function testAutocompletionWithLimit(){ + /** @var \Drupal\linkit\MatcherInterface $plugin */ + $plugin = $this->matcherManager->createInstance('entity:entity_test'); + $configuration = $plugin->getConfiguration(); + $configuration['settings']['limit'] = 2; + + $this->linkitProfile->addMatcher($configuration); + $this->linkitProfile->save(); + + $entity_1 = EntityTest::create(['name' => 'foo 1']); + $entity_1->save(); + $entity_2 = EntityTest::create(['name' => 'foo 2']); + $entity_2->save(); + $entity_3 = EntityTest::create(['name' => 'foo 3']); + $entity_3->save(); + + $data = $this->getAutocompleteResult('foo'); + $this->assertTrue(count($data) == 2, 'Autocomplete returned the expected amount of suggestions.'); + } + /** * Tests autocompletion with translated entities. */