View source
<?php
namespace Drupal\search_api_autocomplete_test\Plugin\search_api_autocomplete\suggester;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Url;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api_autocomplete\SearchInterface;
use Drupal\search_api_autocomplete\Suggester\SuggesterPluginBase;
use Drupal\search_api_autocomplete\Suggestion\SuggestionFactory;
use Drupal\search_api_test\TestPluginTrait;
class TestSuggester extends SuggesterPluginBase implements PluginFormInterface {
use TestPluginTrait;
public static function supportsSearch(SearchInterface $search) {
$key = 'search_api_test.suggester.method.' . __FUNCTION__;
$override = \Drupal::state()
->get($key);
if ($override) {
return call_user_func($override, $search);
}
return TRUE;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$this
->logMethodCall(__FUNCTION__, func_get_args());
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this, $form, $form_state);
}
return [];
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->logMethodCall(__FUNCTION__, func_get_args());
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $form, $form_state);
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->logMethodCall(__FUNCTION__, func_get_args());
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $form, $form_state);
return;
}
$this
->setConfiguration($form_state
->getValues());
}
public function getAutocompleteSuggestions(QueryInterface $query, $incomplete_key, $user_input) {
$this
->logMethodCall(__FUNCTION__, func_get_args());
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this, $query, $incomplete_key, $user_input);
}
$suggestions = [];
$factory = new SuggestionFactory($user_input);
for ($i = 1; $i < $query
->getOption('limit', 10); ++$i) {
$suggestions[] = $factory
->createFromSuggestionSuffix("-suggester-{$i}", $i);
}
$account = \Drupal::currentUser()
->getAccount();
if ($uid = $account
->id()) {
$url = Url::fromRoute('entity.user.canonical', [
'user' => $uid,
]);
}
else {
$url = Url::fromRoute('user.login');
}
$suggestions[] = $factory
->createUrlSuggestion($url, "{$user_input}-suggester-url");
return $suggestions;
}
public function calculateDependencies() {
$this
->logMethodCall(__FUNCTION__, func_get_args());
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this);
}
if (!empty($this->configuration['dependencies'])) {
return $this->configuration['dependencies'];
}
return [];
}
public function onDependencyRemoval(array $dependencies) {
$remove = $this
->getReturnValue(__FUNCTION__, FALSE);
if ($remove) {
unset($this->configuration['dependencies']);
}
return $remove;
}
}