class SearchService in Fast Autocomplete 8
Class SearchService.
@package Drupal\fac
Hierarchy
- class \Drupal\fac\SearchService
Expanded class hierarchy of SearchService
1 file declares its use of SearchService
- FacController.php in src/
Controller/ FacController.php
1 string reference to 'SearchService'
1 service uses SearchService
File
- src/
SearchService.php, line 13
Namespace
Drupal\facView source
class SearchService {
/**
* The search plugin manager service.
*
* @var SearchPluginManager
*/
protected $searchPluginManager;
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* SearchService constructor.
*
* @param \Drupal\fac\SearchPluginManager $search_plugin_manager
* The SearchPluginManager instance.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The EntityTypeManagerInterface instance.
*/
public function __construct(SearchPluginManager $search_plugin_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->searchPluginManager = $search_plugin_manager;
$this->entityTypeManager = $entity_type_manager;
}
/**
* Return the results based on the given key.
*
* @param \Drupal\fac\FacConfigInterface $fac_config
* The FacConfig entity.
* @param string $langcode
* The language code.
* @param string $key
* The search key.
*
* @return array
* The results for the given key.
*/
public function getResults(FacConfigInterface $fac_config, $langcode, $key) {
$search_plugin = $this->searchPluginManager
->createInstance($fac_config
->getSearchPluginId());
$search_results = $search_plugin
->getResults($fac_config, $langcode, $key);
$results = $this
->renderResults($fac_config, $search_results, $langcode);
return $results;
}
/**
* Renders the results in the configured view mode.
*
* @param \Drupal\fac\FacConfigInterface $fac_config
* The FacConfig entity.
* @param array $search_results
* The results to render.
* @param string $langcode
* The language code.
*
* @return array
* The array of rendered items.
*/
protected function renderResults(FacConfigInterface $fac_config, array $search_results, $langcode) {
$results = [];
foreach ($search_results as $search_result) {
try {
$entity = $this->entityTypeManager
->getStorage($search_result['entity_type'])
->load($search_result['entity_id']);
} catch (InvalidPluginDefinitionException $e) {
return [];
}
$view_builder = $this->entityTypeManager
->getViewBuilder($search_result['entity_type']);
$view_modes = $fac_config
->getViewModes();
$build = $view_builder
->view($entity, $view_modes[$search_result['entity_type']], $langcode);
$results[] = render($build);
}
return $results;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SearchService:: |
protected | property | The entity type manager service. | |
SearchService:: |
protected | property | The search plugin manager service. | |
SearchService:: |
public | function | Return the results based on the given key. | |
SearchService:: |
protected | function | Renders the results in the configured view mode. | |
SearchService:: |
public | function | SearchService constructor. |