View source
<?php
namespace Drupal\search\Plugin;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Component\Utility\Unicode;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class SearchPluginBase extends PluginBase implements ContainerFactoryPluginInterface, SearchInterface, RefinableCacheableDependencyInterface {
use RefinableCacheableDependencyTrait;
protected $keywords;
protected $searchParameters;
protected $searchAttributes;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
public function setSearch($keywords, array $parameters, array $attributes) {
$this->keywords = (string) $keywords;
$this->searchParameters = $parameters;
$this->searchAttributes = $attributes;
return $this;
}
public function getKeywords() {
return $this->keywords;
}
public function getParameters() {
return $this->searchParameters;
}
public function getAttributes() {
return $this->searchAttributes;
}
public function isSearchExecutable() {
return !empty($this->keywords);
}
public function getType() {
return NULL;
}
public function buildResults() {
$results = $this
->execute();
$built = [];
foreach ($results as $result) {
$built[] = [
'#theme' => 'search_result',
'#result' => $result,
'#plugin_id' => $this
->getPluginId(),
];
}
return $built;
}
public function searchFormAlter(array &$form, FormStateInterface $form_state) {
}
public function suggestedTitle() {
if (!empty($this->keywords)) {
return $this
->t('Search for @keywords', [
'@keywords' => Unicode::truncate($this->keywords, 60, TRUE, TRUE),
]);
}
return $this
->t('Search');
}
public function buildSearchUrlQuery(FormStateInterface $form_state) {
$keys = trim($form_state
->getValue('keys'));
$query = [
'keys' => $keys,
];
return $query;
}
public function getHelp() {
$help = [
'list' => [
'#theme' => 'item_list',
'#items' => [
$this
->t('Search looks for exact, case-insensitive keywords; keywords shorter than a minimum length are ignored.'),
$this
->t('Use upper-case OR to get more results. Example: cat OR dog (content contains either "cat" or "dog").'),
$this
->t('You can use upper-case AND to require all words, but this is the same as the default behavior. Example: cat AND dog (same as cat dog, content must contain both "cat" and "dog").'),
$this
->t('Use quotes to search for a phrase. Example: "the cat eats mice".'),
$this
->t('You can precede keywords by - to exclude them; you must still have at least one "positive" keyword. Example: cat -dog (content must contain cat and cannot contain dog).'),
],
],
];
return $help;
}
public function usesAdminTheme() {
return $this->pluginDefinition['use_admin_theme'];
}
}