View source
<?php
namespace Drupal\search_extra_type\Plugin\Search;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\UrlGeneratorTrait;
use Drupal\Core\Url;
use Drupal\search\Plugin\ConfigurableSearchPluginBase;
class SearchExtraTypeSearch extends ConfigurableSearchPluginBase {
use UrlGeneratorTrait;
public function setSearch($keywords, array $parameters, array $attributes) {
if (empty($parameters['search_conditions'])) {
$parameters['search_conditions'] = '';
}
parent::setSearch($keywords, $parameters, $attributes);
}
public function isSearchExecutable() {
return (bool) ($this->keywords || !empty($this->searchParameters['search_conditions']));
}
public function execute() {
$results = array();
if (!$this
->isSearchExecutable()) {
return $results;
}
return array(
array(
'link' => Url::fromRoute('test_page_test.test_page')
->toString(),
'type' => 'Dummy result type',
'title' => 'Dummy title',
'snippet' => SafeMarkup::format("Dummy search snippet to display. Keywords: @keywords\n\nConditions: @search_parameters", [
'@keywords' => $this->keywords,
'@search_parameters' => print_r($this->searchParameters, TRUE),
]),
),
);
}
public function buildResults() {
$results = $this
->execute();
$output['prefix']['#markup'] = '<h2>Test page text is here</h2> <ol class="search-results">';
foreach ($results as $entry) {
$output[] = array(
'#theme' => 'search_result',
'#result' => $entry,
'#plugin_id' => 'search_extra_type_search',
);
}
$pager = array(
'#type' => 'pager',
);
$output['suffix']['#markup'] = '</ol>' . drupal_render($pager);
return $output;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['extra_type_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Extra type settings'),
'#tree' => TRUE,
);
$form['extra_type_settings']['boost'] = array(
'#type' => 'select',
'#title' => t('Boost method'),
'#options' => array(
'bi' => t('Bistromathic'),
'ii' => t('Infinite Improbability'),
),
'#default_value' => $this->configuration['boost'],
);
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['boost'] = $form_state
->getValue(array(
'extra_type_settings',
'boost',
));
}
public function defaultConfiguration() {
return array(
'boost' => 'bi',
);
}
}