You are here

public function SiteNameLink::postprocessSearchResults in Search API Field Map 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/processor/SiteNameLink.php \Drupal\search_api_field_map\Plugin\search_api\processor\SiteNameLink::postprocessSearchResults()
  2. 8.2 src/Plugin/search_api/processor/SiteNameLink.php \Drupal\search_api_field_map\Plugin\search_api\processor\SiteNameLink::postprocessSearchResults()
  3. 4.x src/Plugin/search_api/processor/SiteNameLink.php \Drupal\search_api_field_map\Plugin\search_api\processor\SiteNameLink::postprocessSearchResults()

Postprocess search results before they are returned by the query.

If a processor is used for both pre- and post-processing a search query, the same object will be used for both calls (so preserving some data or state locally is possible).

Parameters

\Drupal\search_api\Query\ResultSetInterface $results: The search results.

Overrides ProcessorPluginBase::postprocessSearchResults

File

src/Plugin/search_api/processor/SiteNameLink.php, line 45

Class

SiteNameLink
Links the base url with the site name.

Namespace

Drupal\search_api_field_map\Plugin\search_api\processor

Code

public function postprocessSearchResults(ResultSetInterface $results) {
  $query = $results
    ->getQuery();
  if (!$results
    ->getResultCount()) {
    return;
  }
  $result_items = $results
    ->getResultItems();
  foreach ($result_items as $key => $item) {
    $site = $item
      ->getExtraData('search_api_solr_document')['site'];
    $url = Url::fromUri($site);
    $name = $item
      ->getField('site_name')
      ->getValues()[0];
    $link = Link::fromTextAndUrl(t($name), $url)
      ->toString();
    $item
      ->getField('site_name')
      ->setValues([
      $link,
    ]);
  }
}