Commit 1ff6c1cb authored by nuez's avatar nuez Committed by anon

Issue #2980649 by nuez, Berdir, rbayliss, anon: Add a limit to the query...

Issue #2980649 by nuez, Berdir, rbayliss, anon: Add a limit to the query executed for the autocomplete results
parent 8e5b31a0
......@@ -45,6 +45,8 @@ linkit.matcher.entity:
type: boolean
substitution_type:
type: string
limit:
type: integer
linkit.matcher.entity:*:
type: linkit.matcher.entity
......
......@@ -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;
......
......@@ -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.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment