google_cse.module in Google Custom Search Engine 8.3
Same filename and directory in other branches
Display a Google Custom Search Engine (CSE) on your site.
File
google_cse.moduleView source
<?php
/**
* @file
* Display a Google Custom Search Engine (CSE) on your site.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\search\Entity\SearchPage;
/**
* Implements hook_help().
*/
function google_cse_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.google_cse':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Google Custom Search Engine (CSE) is an embedded search engine that can be used to search any set of one or more sites. No Google API key is required. Read more at <a href="http://www.google.com/cse/." target="blank">http://www.google.com/cse/</a>.') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<p>' . t('After installing this module, activate Google CSE at <a href="/admin/config/search/pages" target="blank">admin/config/search/pages</a>, optionally setting it as the default search module, and configure it by entering Google\'s unique ID for your CSE. Once you have granted permission for one or more roles to search the Google CSE, the search page can be found at search/google, and a separate self-contained search block can also be enabled.') . '</p>';
$output .= '<b>' . t('Blocks') . '</b>';
$output .= '<p>' . t('The include Google CSE block can optionally be enabled at admin/structure/block. The "Google CSE" block provides a search box and also displays the search results. After entering search terms, the user will be returned to the same page (via GET request) and the results will be displayed. Do not allow this Google CSE block to appear on the search/google page, as the search results will fail to display.') . '</p>';
$output .= '<b>' . t('SiteSearch') . '</b>';
$output .= '<p>' . t('In addition to the CSE functionality, SiteSearch on one or more domains or URL paths can optionally be configured. Radio buttons allow users to search on either the SiteSearch option(s) or the CSE, and searches can default to either option.') . '</p>';
$output .= '<b>' . t('Advanced Settings') . '</b>';
$output .= '<p>' . t("The collapsed advanced settings on the settings page provide various customizations such as country and language preferences. For example, with the Locale module enabled, the Google CSE user interface language can be selected dynamically based on the current user's language.") . '</p>';
return $output;
}
}
/**
* Get the relevant language to use for the search.
*
* @return string
* The language.
*/
function google_cse_language() {
$config = \Drupal::config('search.page.google_cse_search')
->get('configuration');
return isset($config['locale_hl']) ? \Drupal::languageManager()
->getCurrentLanguage()
->getId() : $config['hl'];
}
/**
* Implements hook_proxy_settings_info().
*/
function google_cse_proxy_settings_info() {
return [
'google_cse_adv' => [
'name' => 'Google Custom Search Engine',
],
];
}
/**
* Implements hook_theme().
*/
function google_cse_theme($existing, $type, $theme, $path) {
return [
'google_cse_results' => [
'variables' => [
'form' => FALSE,
'path' => $path,
],
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_results',
],
// @TODO confirm the placement of template file.
// @see https://www.drupal.org/node/2853472
'google_cse_adv_results' => [
'variables' => [
'form' => FALSE,
'path' => $path,
],
'file' => 'google_cse.theme.inc',
'template' => 'google_cse_adv/templates/google_cse_adv_results',
],
];
}
/**
* Implements hook_preprocess_item_list__search_results().
*/
function google_cse_preprocess_item_list__search_results(&$variables) {
// In the context of Google CSE, we rely on Google to render the content.
// Therefore, we do not want to use Drupal's default list
// (see Drupal\search\Controller\SearchController::view()).
// The simplest way to achieve this in a Drupal context is to
// unset the "items" sent to the list template and render the
// Google CSE results as the 'empty' value.
if (isset($variables['items'][0]['value'])) {
if ($variables['items'][0]['value']['#theme'] == "google_cse_results") {
$variables['empty'] = $variables['items'][0];
unset($variables['empty']['attributes']);
unset($variables['items']);
}
}
}
/**
* Implements hook_library_info_build().
*/
function google_cse_library_info_build() {
$libraries = [];
$config = \Drupal::config('search.page.google_cse_search');
// Add the custom css.
if (Drupal::moduleHandler()
->moduleExists('google_cse')) {
$libraries['google_cse/googlecseCustomcss'] = [
'css' => [
'theme' => [
$config
->get('configuration')['custom_css'] => [
'type' => 'external',
],
],
],
];
}
return $libraries;
}
/**
* Adds custom submit handler for search block form.
*/
function google_cse_form_search_block_form_alter(&$form, &$form_state, $form_id) {
$config = \Drupal::config('search.page.google_cse_search');
$activeSearchPages = \Drupal::service('search.search_page_repository');
if (array_key_exists('google_cse_search', $activeSearchPages
->getActiveSearchPages())) {
$default = $activeSearchPages
->getDefaultSearchPage();
$type = \Drupal::service('plugin.manager.search');
$plugin_definition = $type
->getDefinition($default);
\Drupal::service('google_cse.services')
->siteSearchForm($form);
$form['#attributes']['class'][] = 'google-cse';
$form['#attached']['library'][] = 'google_cse/googlecseWatermark';
$form['#attached']['drupalSettings']['googleCSE'] = [
'cx' => $config
->get('configuration')['cx'],
'language' => google_cse_language(),
'resultsWidth' => intval($config
->get('configuration')['results_width']),
'domain' => $config
->get('configuration')['domain'],
'isDefault' => $plugin_definition['provider'] === "google_cse" ? TRUE : FALSE,
];
}
}
/**
* Implements hook_entity_insert().
*
* Clear appropriate caches on storing Google CSE search plugin entity instance.
*/
function google_cse_entity_insert(EntityInterface $entity) {
if ($entity instanceof SearchPage) {
$plugin = $entity
->getPlugin()
->getPluginId();
if ($plugin == 'google_cse_search') {
\Drupal::service('router.builder')
->rebuild();
}
}
}
Functions
Name | Description |
---|---|
google_cse_entity_insert | Implements hook_entity_insert(). |
google_cse_form_search_block_form_alter | Adds custom submit handler for search block form. |
google_cse_help | Implements hook_help(). |
google_cse_language | Get the relevant language to use for the search. |
google_cse_library_info_build | Implements hook_library_info_build(). |
google_cse_preprocess_item_list__search_results | Implements hook_preprocess_item_list__search_results(). |
google_cse_proxy_settings_info | Implements hook_proxy_settings_info(). |
google_cse_theme | Implements hook_theme(). |