class TestBackend in Search API 8
Provides a dummy backend for testing purposes.
Plugin annotation
@SearchApiBackend(
id = "search_api_test",
label = @Translation("Test backend"),
description = @Translation("Dummy backend implementation")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\search_api\Plugin\HideablePluginBase implements HideablePluginInterface
- class \Drupal\search_api\Plugin\ConfigurablePluginBase implements ConfigurablePluginInterface uses PluginDependencyTrait
- class \Drupal\search_api\Backend\BackendPluginBase implements BackendInterface uses LoggerTrait
- class \Drupal\search_api_test\Plugin\search_api\backend\TestBackend implements PluginFormInterface uses PluginFormTrait, TestPluginTrait
- class \Drupal\search_api\Backend\BackendPluginBase implements BackendInterface uses LoggerTrait
- class \Drupal\search_api\Plugin\ConfigurablePluginBase implements ConfigurablePluginInterface uses PluginDependencyTrait
- class \Drupal\search_api\Plugin\HideablePluginBase implements HideablePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of TestBackend
1 file declares its use of TestBackend
- BackendPluginBaseTest.php in tests/
src/ Unit/ BackendPluginBaseTest.php
File
- tests/
search_api_test/ src/ Plugin/ search_api/ backend/ TestBackend.php, line 23
Namespace
Drupal\search_api_test\Plugin\search_api\backendView source
class TestBackend extends BackendPluginBase implements PluginFormInterface {
use PluginFormTrait;
use TestPluginTrait {
checkError as traitCheckError;
}
/**
* {@inheritdoc}
*/
public function postInsert() {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this);
return;
}
$this
->checkError(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function preUpdate() {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this);
return;
}
$this
->checkError(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function postUpdate() {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this);
}
$this
->checkError(__FUNCTION__);
return $this
->getReturnValue(__FUNCTION__, FALSE);
}
/**
* {@inheritdoc}
*/
public function viewSettings() {
return [
[
'label' => 'Dummy Info',
'info' => 'Dummy Value',
'status' => 'error',
],
[
'label' => 'Dummy Info 2',
'info' => 'Dummy Value 2',
],
];
}
/**
* {@inheritdoc}
*/
public function getSupportedFeatures() {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this);
}
return [
'search_api_mlt',
];
}
/**
* {@inheritdoc}
*/
public function supportsDataType($type) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this, $type);
}
return in_array($type, [
'search_api_test',
'search_api_test_altering',
]);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'test' => '',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['test'] = [
'#type' => 'textfield',
'#title' => 'Test',
'#default_value' => $this->configuration['test'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function indexItems(IndexInterface $index, array $items) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this, $index, $items);
}
$this
->checkError(__FUNCTION__);
$state = \Drupal::state();
$key = 'search_api_test.backend.indexed.' . $index
->id();
$indexed_values = $state
->get($key, []);
$skip = $state
->get('search_api_test.backend.indexItems.skip', []);
$skip = array_flip($skip);
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($items as $id => $item) {
if (isset($skip[$id])) {
unset($items[$id]);
continue;
}
$indexed_values[$id] = [];
foreach ($item
->getFields() as $field_id => $field) {
$indexed_values[$id][$field_id] = $field
->getValues();
}
}
$state
->set($key, $indexed_values);
return array_keys($items);
}
/**
* {@inheritdoc}
*/
public function addIndex(IndexInterface $index) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $index);
return;
}
$this
->checkError(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function updateIndex(IndexInterface $index) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $index);
return;
}
$this
->checkError(__FUNCTION__);
$index
->reindex();
}
/**
* {@inheritdoc}
*/
public function removeIndex($index) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $index);
return;
}
$this
->checkError(__FUNCTION__);
}
/**
* {@inheritdoc}
*/
public function deleteItems(IndexInterface $index, array $item_ids) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $index, $item_ids);
return;
}
$this
->checkError(__FUNCTION__);
$state = \Drupal::state();
$key = 'search_api_test.backend.indexed.' . $index
->id();
$indexed_values = $state
->get($key, []);
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach ($item_ids as $item_id) {
unset($indexed_values[$item_id]);
}
$state
->set($key, $indexed_values);
}
/**
* {@inheritdoc}
*/
public function deleteAllIndexItems(IndexInterface $index, $datasource_id = NULL) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $index, $datasource_id);
return;
}
$this
->checkError(__FUNCTION__);
$key = 'search_api_test.backend.indexed.' . $index
->id();
if (!$datasource_id) {
\Drupal::state()
->delete($key);
return;
}
$indexed = \Drupal::state()
->get($key, []);
/** @var \Drupal\search_api\Item\ItemInterface $item */
foreach (array_keys($indexed) as $item_id) {
list($item_datasource_id) = Utility::splitCombinedId($item_id);
if ($item_datasource_id == $datasource_id) {
unset($indexed[$item_id]);
}
}
\Drupal::state()
->set($key, $indexed);
}
/**
* {@inheritdoc}
*/
public function search(QueryInterface $query) {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
call_user_func($override, $this, $query);
return;
}
$this
->checkError(__FUNCTION__);
$results = $query
->getResults();
$result_items = [];
$datasources = $query
->getIndex()
->getDatasources();
/** @var \Drupal\search_api\Datasource\DatasourceInterface $datasource */
$datasource = reset($datasources);
$datasource_id = $datasource
->getPluginId();
$fields_helper = \Drupal::getContainer()
->get('search_api.fields_helper');
if ($query
->getKeys() && $query
->getKeys()[0] == 'test') {
$item_id = Utility::createCombinedId($datasource_id, '1');
$item = $fields_helper
->createItem($query
->getIndex(), $item_id, $datasource);
$item
->setScore(2);
$item
->setExcerpt('test');
$result_items[$item_id] = $item;
}
elseif ($query
->getOption('search_api_mlt')) {
$item_id = Utility::createCombinedId($datasource_id, '2');
$item = $fields_helper
->createItem($query
->getIndex(), $item_id, $datasource);
$item
->setScore(2);
$item
->setExcerpt('test test');
$result_items[$item_id] = $item;
}
else {
$item_id = Utility::createCombinedId($datasource_id, '1');
$item = $fields_helper
->createItem($query
->getIndex(), $item_id, $datasource);
$item
->setScore(1);
$result_items[$item_id] = $item;
$item_id = Utility::createCombinedId($datasource_id, '2');
$item = $fields_helper
->createItem($query
->getIndex(), $item_id, $datasource);
$item
->setScore(1);
$result_items[$item_id] = $item;
}
$results
->setResultItems($result_items);
$results
->setResultCount(count($result_items));
}
/**
* {@inheritdoc}
*/
public function isAvailable() {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
return call_user_func($override, $this);
}
return $this
->getReturnValue(__FUNCTION__, TRUE);
}
/**
* {@inheritdoc}
*/
public function getDiscouragedProcessors() {
if ($override = $this
->getMethodOverride(__FUNCTION__)) {
// Safeguard against "stupid" dummy methods used in tests, such as
// \Drupal\search_api_test\MethodOverrides::overrideTestBackendMethod().
$ret = call_user_func($override, $this);
return is_array($ret) ? $ret : [];
}
return $this
->getReturnValue(__FUNCTION__, []);
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return $this->configuration['dependencies'] ?? [];
}
/**
* {@inheritdoc}
*/
public function onDependencyRemoval(array $dependencies) {
$remove = $this
->getReturnValue(__FUNCTION__, FALSE);
if ($remove) {
unset($this->configuration['dependencies']);
}
return $remove;
}
/**
* {@inheritdoc}
*/
protected function checkError($method) {
$this
->traitCheckError($method);
$this
->logMethodCall($method);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BackendPluginBase:: |
protected | property | The fields helper. | |
BackendPluginBase:: |
protected | property |
The messenger. Overrides MessengerTrait:: |
|
BackendPluginBase:: |
protected | property | The server this backend is configured for. | |
BackendPluginBase:: |
protected | property | The backend's server's ID. | |
BackendPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ConfigurablePluginBase:: |
1 |
BackendPluginBase:: |
public | function |
Provides information on additional fields made available by the backend. Overrides BackendSpecificInterface:: |
|
BackendPluginBase:: |
public | function | Retrieves the fields helper. | |
BackendPluginBase:: |
public | function | Retrieves the messenger. | |
BackendPluginBase:: |
protected | function | Retrieves the effective fulltext fields from the query. | |
BackendPluginBase:: |
public | function |
Retrieves the server entity for this backend. Overrides BackendInterface:: |
|
BackendPluginBase:: |
protected | function | Creates dummy field objects for the "magic" fields present for every index. | 1 |
BackendPluginBase:: |
public | function |
Notifies the backend that the server is about to be deleted. Overrides BackendInterface:: |
1 |
BackendPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginBase:: |
|
BackendPluginBase:: |
public | function | Sets the fields helper. | |
BackendPluginBase:: |
public | function |
Sets the messenger. Overrides MessengerTrait:: |
|
BackendPluginBase:: |
public | function |
Sets the server entity for this backend. Overrides BackendInterface:: |
|
BackendPluginBase:: |
protected | function | Verifies that the given condition operator is valid for this backend. | |
BackendPluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides ConfigurablePluginBase:: |
1 |
BackendPluginBase:: |
public | function |
Implements the magic __sleep() method. Overrides DependencySerializationTrait:: |
1 |
BackendPluginBase:: |
public | function |
Implements the magic __wakeup() method. Overrides DependencySerializationTrait:: |
1 |
ConfigurablePluginBase:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | |
ConfigurablePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ConfigurablePluginBase:: |
public | function |
Returns the plugin's description. Overrides ConfigurablePluginInterface:: |
|
ConfigurablePluginBase:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
ConfigurablePluginBase:: |
public | function |
Returns the label for use on the administration pages. Overrides ConfigurablePluginInterface:: |
|
ConfigurablePluginBase:: |
protected | function | Wraps the module handler. | |
ConfigurablePluginBase:: |
protected | function | Wraps the theme handler. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
HideablePluginBase:: |
public | function |
Determines whether this plugin should be hidden in the UI. Overrides HideablePluginInterface:: |
1 |
LoggerTrait:: |
protected | property | The logging channel to use. | |
LoggerTrait:: |
public | function | Retrieves the logger. | |
LoggerTrait:: |
protected | function | Logs an exception. | |
LoggerTrait:: |
public | function | Sets the logger. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. Aliased as: traitCalculatePluginDependencies | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. Aliased as: traitGetPluginDependencies | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. Aliased as: traitModuleHandler | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. Aliased as: traitThemeHandler | 1 |
PluginFormTrait:: |
public | function | Form submission handler. | 7 |
PluginFormTrait:: |
public | function | Form validation handler. | 2 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TestBackend:: |
public | function |
Adds a new index to this server. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
TestBackend:: |
public | function |
Calculates dependencies for the configured plugin. Overrides ConfigurablePluginBase:: |
|
TestBackend:: |
protected | function | ||
TestBackend:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurablePluginBase:: |
|
TestBackend:: |
public | function |
Deletes all the items from the index. Overrides BackendSpecificInterface:: |
|
TestBackend:: |
public | function |
Deletes the specified items from the index. Overrides BackendSpecificInterface:: |
|
TestBackend:: |
public | function |
Limits the processors displayed in the UI for indexes on this server. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Returns all features that this backend supports. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Indexes the specified items. Overrides BackendSpecificInterface:: |
|
TestBackend:: |
public | function |
Returns a boolean with the availability of the backend. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Informs the plugin that some of its dependencies are being removed. Overrides ConfigurablePluginBase:: |
|
TestBackend:: |
public | function |
Reacts to the server's creation. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Notifies the backend that its configuration was updated. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Notifies the backend that its configuration is about to be updated. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Removes an index from this server. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Executes a search on this server. Overrides BackendSpecificInterface:: |
|
TestBackend:: |
public | function |
Determines whether the backend supports a given add-on data type. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Notifies the server that an index attached to it has been changed. Overrides BackendPluginBase:: |
|
TestBackend:: |
public | function |
Returns additional, backend-specific information about this server. Overrides BackendPluginBase:: |
|
TestPluginTrait:: |
protected | property | This object's plugin type. | |
TestPluginTrait:: |
protected | function | Throws an exception if set in the Drupal state for the given method. Aliased as: traitCheckError | |
TestPluginTrait:: |
protected | function | Retrieves a possible override set for the given method. | |
TestPluginTrait:: |
protected | function | Returns the plugin type of this object. | |
TestPluginTrait:: |
protected | function | Retrieves the value to return for a certain method. | |
TestPluginTrait:: |
protected | function | Logs a method call to the site state. | |
TestPluginTrait:: |
public | function | Implements the magic __call() method. |