You are here

google_appliance.module in Google Search Appliance 8

Google Appliance module file.

File

google_appliance.module
View source
<?php

/**
 * @file
 * Google Appliance module file.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\google_appliance\Routing\SearchViewRoute;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function google_appliance_help($route_name, RouteMatchInterface $route_match) {
  $output = '';
  switch ($route_name) {
    case 'help.page.google_appliance':
      $output .= '<h2>' . t('About') . '</h2>' . PHP_EOL;
      $output .= '<p>' . t('The Google Appliance module provides an interface to obtain search results from a dedicated Google Search Appliance (GSA) hardware device. Please visit the <a href="@project-page">Google Appliance Project Page</a> to learn more about features and development notes.') . '</p>' . PHP_EOL;
      $output .= '<h2>' . t('Setup and Usage') . '</h2>' . PHP_EOL;
      $output .= '<p>' . t('First, you must set up a collection and frontend on your GSA device that can be accessed publicly. Then configure the connection information on the <a href="@settings-page">Google Appliance Settings page</a> To produce results that include Drupal content, the GSA crawl must have crawled your drupal installation and indexed the desired content. No cron-job configuration is required (ala core Search) since index management is handled by the GSA device.', [
        '@settings-page' => '/admin/config/search/google_appliance/settings',
      ]) . '</p>' . PHP_EOL;
      $output .= '<h3>' . t('Teaming up with core Search') . '</h3>';
      $output .= '<p>' . t('The Google Appliance module can act as a replacement for core <i>Search</i>, or it may operate in tandem with it. Say you want to provide search results for the public to include both drupal and non-drupal content, but you want administrative searches to only provide results for local site content. In this case you use the Google Appliance searches for public search, and core Search for admin searches.') . '</p>' . PHP_EOL;
      $output .= '<p>' . t('If you will use core search along with this module, core search results will show up normally at <code>search/{search terms}</code>, while Google Appliance searches will show up by default at <code>gsearch/{search terms}</code>.') . '</p>' . PHP_EOL;
      $output .= '<h2>' . t('Blocks') . '</h2>';
      $output .= '<p>' . t('The module provides two blocks:') . '</p>' . PHP_EOL;
      $output .= '<ol><li>' . t('the block search form') . '</li>' . PHP_EOL;
      $output .= '<li>' . t('the related searches block') . '</li></ol>' . PHP_EOL;
      $output .= '<p>' . t('Both can be enabled and configured on the <a href="@blocks-admin">Blocks administration page</a>. The block search form provides a search form that can be placed into any block. The related searches block displays a list of links to related results. The related search block only makes sense to appear on results pages, so it should be configured accordingly (i.e. specify <code>gsearch*</code> as the pages for the block to appear on).', [
        '@blocks-admin' => Url::fromRoute('block.admin_display'),
      ]) . '</p>';
      break;
    case 'google_appliance.admin':
      $output = '<p>' . t('The Google Appliance settings specify the interface to your GSA device. The search results obtained via that interface are controlled by the Google Search Appliance configuration itself. Visit the <a href="@gsa-help">Google Appliance help section</a> for more information on setting up the integration.', [
        '@gsa-help' => Url::fromRoute('help.page.google_appliance'),
      ]) . '</p>';
      break;
  }
  return $output;
}

/**
 * Implements hook_theme().
 */
function google_appliance_theme() {
  $theme = [];
  $theme['google_appliance_search_results'] = [
    'variables' => [
      'results' => NULL,
      'form' => '',
    ],
  ];
  return $theme;
}

/**
 * Implements hook_page_attachments().
 */
function google_appliance_page_attachments(array &$attachments) {

  // @todo This needs to be finished and tested.
  $gsaSettings = Drupal::configFactory()
    ->get('google_appliance.settings')
    ->get();

  // If configured, add JSON-LD for Google sitelinks search box.
  if (isset($gsaSettings['display_settings']['sitelinks_search_box']) && $gsaSettings['display_settings']['sitelinks_search_box'] === TRUE && Drupal::service('path.matcher')
    ->isFrontPage()) {
    $request = Drupal::request();
    $canonicalUrl = $request
      ->getSchemeAndHttpHost() . $request
      ->getBasePath();

    // Google recommended practices suggest that the "url" key in the JSON-LD
    // and the homepage canonical URL must match. This checks for the canonical
    // metatag value and substitutes in place of the default provided above.
    if (isset($attachments['metatag_canonical']['#value'])) {
      $canonicalUrl = $attachments['metatag_canonical']['#value'];
    }
    $targetUrl = Url::fromRoute(SearchViewRoute::ROUTE_NAME)
      ->toString(TRUE)
      ->getGeneratedUrl() . '/{search_query}';
    $jsld = [
      '@context' => 'http://schema.org',
      '@type' => 'WebSite',
      'url' => $canonicalUrl,
      'potentialAction' => [
        '@type' => 'SearchAction',
        'target' => $targetUrl,
        'query-input' => 'required name=search_term_string',
      ],
    ];
    if (empty($attachments['#attached'])) {
      $attachments['#attached'] = [];
    }
    if (empty($attachments['#attached']['html_head'])) {
      $attachments['#attached']['html_head'] = [];
    }
    $jsldAttachment = [
      '#type' => 'html_tag',
      '#tag' => 'script',
      '#attributes' => [
        'type' => 'application/ld+json',
      ],
      '#value' => Json::encode($jsld),
    ];
    $attachments['#attached']['html_head'][] = [
      $jsldAttachment,
      'google_appliance_slsb',
    ];
  }
}