summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornuez2018-12-28 02:20:00 (GMT)
committerEmil Stjerneman2018-12-28 02:20:00 (GMT)
commit1ff6c1cbdbe6197e0a3bb44e5be2c63ebfdf6849 (patch)
treeaf896159c5e85e3cb5c6de4f6b8fe0e915cd3c67
parent8e5b31a0e6809b82a46ec9623f633eba4780f1c6 (diff)
Issue #2980649 by nuez, Berdir, rbayliss, anon: Add a limit to the query executed for the autocomplete results
-rw-r--r--config/schema/linkit.schema.yml2
-rw-r--r--src/Plugin/Linkit/Matcher/EntityMatcher.php35
-rw-r--r--tests/src/Kernel/LinkitAutocompleteTest.php23
3 files changed, 60 insertions, 0 deletions
diff --git a/config/schema/linkit.schema.yml b/config/schema/linkit.schema.yml
index 882a2f1..c6e3f1d 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 a8a85c8..4231b50 100644
--- a/src/Plugin/Linkit/Matcher/EntityMatcher.php
+++ b/src/Plugin/Linkit/Matcher/EntityMatcher.php
@@ -37,6 +37,11 @@ class EntityMatcher extends ConfigurableMatcherBase {
use MatcherTokensTrait;
/**
+ * The default limit for matches.
+ */
+ const DEFAULT_LIMIT = 100;
+
+ /**
* The database connection.
*
* @var \Drupal\Core\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 912d777..e74d87e 100644
--- a/tests/src/Kernel/LinkitAutocompleteTest.php
+++ b/tests/src/Kernel/LinkitAutocompleteTest.php
@@ -153,6 +153,29 @@ class LinkitAutocompleteTest extends LinkitKernelTestBase {
}
/**
+ * 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.
*/
public function testAutocompletionTranslations() {