SearchService.php in Fast Autocomplete 8
File
src/SearchService.php
View source
<?php
namespace Drupal\fac;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class SearchService {
protected $searchPluginManager;
protected $entityTypeManager;
public function __construct(SearchPluginManager $search_plugin_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->searchPluginManager = $search_plugin_manager;
$this->entityTypeManager = $entity_type_manager;
}
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;
}
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;
}
}