You are here

public function Search::getLabels in Google Site Search 8

Gets render array for labels.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request.

Return value

array A render array.

File

src/Plugin/Search/Search.php, line 679

Class

Search
Handles search using Google Search Engine.

Namespace

Drupal\gss\Plugin\Search

Code

public function getLabels(Request $request) {

  // Don't output anything if there are no labels to display.
  if (empty($this->labels)) {
    return [];
  }

  // Generate a URL for the current search page and extract the active label,
  // if any.
  $url = Url::createFromRequest($request);
  $search_query_parameters = $this
    ->getParameters();
  $active_label = NULL;
  if (isset($search_query_parameters['label']) && $search_query_parameters['label']) {
    $active_label = $search_query_parameters['label'];
    unset($search_query_parameters['label']);
  }
  $build = [
    '#theme' => 'item_list__gss_labels',
    '#title' => $this
      ->t('Show only results of type:'),
    '#wrapper_attributes' => [
      'class' => [
        'search-labels',
      ],
    ],
    '#context' => [
      'list_style' => 'comma-list',
    ],
  ];

  // Add the 'All results' option.
  $build['#items'][] = [
    '#title' => $this
      ->t('All results'),
    '#type' => 'link',
    '#url' => $url
      ->setOption('query', $search_query_parameters),
  ];

  // Add each label as an option.
  foreach ($this->labels as $facet_set) {
    foreach ($facet_set as $facet) {
      $url = clone $url;

      // Add the current label as a search parameter.
      $search_query_parameters['label'] = $facet->label;
      $url
        ->setOption('query', $search_query_parameters);
      $url
        ->setOption('attributes', $active_label == $facet->label ? [
        'class' => [
          'is-active',
        ],
      ] : []);
      $build['#items'][] = [
        '#title' => $this
          ->t($facet->anchor),
        '#type' => 'link',
        '#url' => $url,
      ];
    }
  }
  return $build;
}