You are here

class SearchService in Fast Autocomplete 8

Class SearchService.

@package Drupal\fac

Hierarchy

Expanded class hierarchy of SearchService

1 file declares its use of SearchService
FacController.php in src/Controller/FacController.php
1 string reference to 'SearchService'
fac.services.yml in ./fac.services.yml
fac.services.yml
1 service uses SearchService
fac.search_service in ./fac.services.yml
Drupal\fac\SearchService

File

src/SearchService.php, line 13

Namespace

Drupal\fac
View 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

Namesort descending Modifiers Type Description Overrides
SearchService::$entityTypeManager protected property The entity type manager service.
SearchService::$searchPluginManager protected property The search plugin manager service.
SearchService::getResults public function Return the results based on the given key.
SearchService::renderResults protected function Renders the results in the configured view mode.
SearchService::__construct public function SearchService constructor.