You are here

function google_appliance_preprocess_html in Google Search Appliance 7

Implements hook_preprocess_html().

File

./google_appliance.module, line 201
Google Appliance module enables searching via a dedicated Google Search Appliance hardware device. See README.txt and the help page at admin/help/google_appliance.

Code

function google_appliance_preprocess_html(&$variables) {
  $settings = _google_appliance_get_settings();

  // If configured, add JSON-LD for Google sitelinks search box.
  if ($settings['sitelinks_search_box'] && drupal_is_front_page()) {
    $canonicalUrl = $GLOBALS['base_url'] . base_path();

    // 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.
    $knownElements = drupal_add_html_head();
    if (isset($knownElements['metatag_canonical']['#value'])) {
      $canonicalUrl = $knownElements['metatag_canonical']['#value'];
    }

    // Build the JSON-LD object.
    $jsld = array(
      '@context' => 'http://schema.org',
      '@type' => 'WebSite',
      'url' => $canonicalUrl,
      'potentialAction' => array(
        '@type' => 'SearchAction',
        'target' => url($settings['drupal_path'], array(
          'absolute' => TRUE,
        )) . '/{search_term_string}',
        'query-input' => 'required name=search_term_string',
      ),
    );

    // Add the data as markup to the HTML head. If necessary, you can alter this
    // JSON-LD via hook_html_head_alter() with the key "google_appliance_slsb."
    drupal_add_html_head(array(
      '#type' => 'markup',
      '#markup' => '<script type="application/ld+json">' . drupal_json_encode($jsld) . '</script>',
      '_json' => $jsld,
    ), 'google_appliance_slsb');
  }
}