You are here

google_appliance.api.php in Google Search Appliance 8

Same filename and directory in other branches
  1. 7 google_appliance.api.php

Hook documentation.

This file contains no working PHP code; it exists to provide additional documentation for doxygen as well as to document hooks in the standard Drupal manner.

File

google_appliance.api.php
View source
<?php

/**
 * @file
 * Hook documentation.
 *
 * This file contains no working PHP code; it exists to provide additional
 * documentation for doxygen as well as to document hooks in the standard
 * Drupal manner.
 */
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\google_appliance\SearchResults\ResultSet;
use Drupal\google_appliance\SearchResults\Synonym;

/**
 * @defgroup google_appliance Google Search Appliance module integrations.
 *
 * Module integrations with the Google Search Appliance module.
 */

/**
 * @defgroup google_appliance_hooks Google Search Appliance's hooks
 * @{
 * Hooks that can be implemented by other modules in order to extend the Google
 * Search Appliance module.
 */

/**
 * Alter Google Search Appliance queries.
 *
 * This hook is invoked after Google Search Appliance builds the query, just
 * before the query is sent to the appliance.
 *
 * Use this to alter which collection is queried, which frontend is used, add
 * meta tag constraints, restrict queries to specific languages, etc. A
 * complete
 * list of search parameters is available in the Google developer documentation
 * referenced below.
 *
 * @param array $params
 *   The search params before they're sent to the host.
 *
 * @see \Drupal\google_appliance\Service\Search::search
 * @see https://developers.google.com/search-appliance/documentation/614/xml_reference#request_parameters
 */
function hook_google_appliance_query_alter(array &$params) {

  // Only return English language results.
  $params['lr'] = 'en';
}

/**
 * Alter Google Search Appliance results.
 *
 * This hook is invoked after the search appliance returns with a response and
 * the Google Search Appliance module does its basic parsing.
 *
 * Use this to make result alterations that are inappropriate at the theme
 * level.
 *
 * @param \Drupal\google_appliance\SearchResults\ResultSet $results
 *   The search results.
 *
 * @see \Drupal\google_appliance\Service\Search::search
 */
function hook_google_appliance_response_alter(ResultSet $results) {
  $results
    ->addSynonym(new Synonym('Foo', 'foo'));
}

/**
 * Alter the cluster list render array containing related searches.
 *
 * This hook is invoked after the list render array is constructed and just
 * before it is passed to drupal_render().
 *
 * Use this to alter the render array properties.
 *
 * @param array $cluster_list
 *   A renderable array conforming to theme_item_list().
 * @param array $cluster_results
 *   The raw cluster results returned via the Google Appliance instance.
 *
 * @see google_appliance_get_clusters()
 * @see theme_item_list()
 *
 * @todo Update as appropriate.
 */
function hook_google_appliance_cluster_list_alter(array &$cluster_list, array $cluster_results) {

  // Add some CSS classes.
  $cluster_list['#attributes']['class'][] = 'foo-list';
  $cluster_list['#items'] = [];

  // Construct a new list of links using the raw results with a custom path.
  foreach ($cluster_results as $cluster) {
    $cluster_list['#items'][] = Link::fromTextAndUrl($cluster['label'], Url::fromUri('search/my/path/' . $cluster['label']))
      ->toString();
  }

  // Change the first item of the list.
  $cluster_list['#items'][0] = '<span>A new item</span>';
}

/**
 * @}
 */

Functions

Namesort descending Description
hook_google_appliance_cluster_list_alter Alter the cluster list render array containing related searches.
hook_google_appliance_query_alter Alter Google Search Appliance queries.
hook_google_appliance_response_alter Alter Google Search Appliance results.