You are here

function google_appliance_get_clusters in Google Search Appliance 8

Get related search via the Google Search Appliance clustering service.

Return value

themed list of links

File

src/Service/Search.php, line 119

Namespace

Drupal\google_appliance\Service

Code

function google_appliance_get_clusters() {

  // Grab module settings.
  $settings = _google_appliance_get_settings();

  // Get the search query.
  $query_pos = substr_count($settings['drupal_path'], '/') + 1;
  $search_query = urldecode(arg($query_pos));
  $cluster_content = NULL;

  // Perform POST to acquire the clusters  block.
  $clusterQueryURL = Html::escape($settings['hostname'] . '/cluster');
  $clusterQueryParams = [
    'q' => Html::escape($search_query),
    'btnG' => 'Google+Search',
    'access' => 'p',
    'entqr' => '0',
    'ud' => '1',
    'sort' => 'date:D:L:d1',
    'output' => 'xml_no_dtd',
    'oe' => 'utf8',
    'ie' => 'utf8',
    'site' => Html::escape($settings['collection']),
    'client' => Html::escape($settings['frontend']),
  ];

  // Alter request according to language filter settings.
  if (Drupal::moduleHandler()
    ->moduleExists('locale') && $settings['language_filter_toggle']) {
    $clusterQueryParams['lr'] = _google_appliance_get_lr($settings['language_filter_options']);
  }

  // cURL request for the clusters produces JSON result.
  $gsa_clusters_json = _curl_post($clusterQueryURL, $clusterQueryParams);

  // No error -> get the clusters.
  if (!$gsa_clusters_json['is_error']) {
    $clusters = json_decode($gsa_clusters_json['response'], TRUE);
    if (isset($clusters['clusters'][0])) {

      // Build the link list.
      $cluster_list_items = [];
      foreach ($clusters['clusters'][0]['clusters'] as $cluster) {

        // @FIXME
        // l() expects a Url object, created from a route name or external URI.
        // array_push($cluster_list_items, l($cluster['label'], $settings['drupal_path'] . '/' . $cluster['label']));
      }

      // Create theme-friendly list of links render array.
      $cluster_list = [
        '#theme' => 'item_list',
        '#items' => $cluster_list_items,
        '#title' => NULL,
        '#type' => 'ul',
        '#attributes' => [],
      ];

      // Allow implementation of hook_google_appliance_cluster_list_alter() by
      // other modules.
      Drupal::moduleHandler()
        ->alter('google_appliance_cluster_list', $cluster_list, $cluster_results);
      $cluster_content = Drupal::service('renderer')
        ->render($cluster_list);
    }
  }
  return $cluster_content;
}