Loading gss.api.php 0 → 100644 +44 −0 Changes for gss.api.php: 44 added lines, 0 removed lines. Original line number Diff line number Diff line <?php /** * @file * Hooks specific to the Google Site Search module. */ /** * @addtogroup hooks * @{ */ /** * Act on a GSS search result being displayed. * * This hook is invoked from the gss search plugin during search execution, * after retrieving the search results. * * @param stdClass $result * The search result item from Google Site Search API. * * @return array * Extra information to be displayed with search result. This information * should be presented as an associative array. It will be concatenated with * the post information (last updated, author) in the default search result * theming. * * @see template_preprocess_search_result() * @see search-result.html.twig */ function hook_gss_search_result($result) { if (isset($result->pagemap->cse_thumbnail[0])) { return [ 'cse_thumbnail' => [ '#theme' => 'image', '#uri' => $result->pagemap->cse_thumbnail[0]->src, ], ]; } } /** * @} End of "addtogroup hooks". */ gss.module +14 −0 Changes for gss.module: 14 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -23,3 +23,17 @@ function gss_help($route_name, RouteMatchInterface $route_match) { return ''; } } /** * Implements hook_gss_search_result(). */ function gss_gss_search_result($result) { if (isset($result->pagemap->cse_thumbnail[0])) { return [ 'cse_thumbnail' => [ '#theme' => 'image', '#uri' => $result->pagemap->cse_thumbnail[0]->src, ], ]; } } src/Plugin/Search/Search.php +89 −19 Changes for src/Plugin/Search/Search.php: 89 added lines, 19 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\gss\Plugin\Search; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Random; use Drupal\Core\Access\AccessResult; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Session\AccountInterface; Loading Loading @@ -55,6 +56,13 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface */ protected $languageManager; /** * A module manager object. * * @var \Drupal\Core\Extension\ModuleHandlerInterface */ protected $moduleHandler; /** * HTTP client. * Loading @@ -78,6 +86,7 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface $plugin_id, $plugin_definition, $container->get('language_manager'), $container->get('module_handler'), $container->get('http_client'), $container->get('key.repository') ); Loading @@ -94,13 +103,16 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface * The plugin implementation definition. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * A module manager object. * @param \GuzzleHttp\Client $http_client * The http client. * @param \Drupal\key\KeyRepository $key_repository * The key repository. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, Client $http_client, KeyRepository $key_repository) { public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, Client $http_client, KeyRepository $key_repository) { $this->languageManager = $language_manager; $this->moduleHandler = $module_handler; $this->httpClient = $http_client; $this->keyRepository = $key_repository; parent::__construct($configuration, $plugin_id, $plugin_definition); Loading Loading @@ -385,7 +397,8 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface */ protected function generateSampleItems($base_url, $options) { drupal_set_message($this->t('GSS Debug mode enabled, visit the <a href=":settings">search page settings</a> to disable.', [ ':settings' => Url::fromRoute('entity.search_page.edit_form', ['search_page' => $this->searchPageId])->toString(), ':settings' => Url::fromRoute('entity.search_page.edit_form', ['search_page' => $this->searchPageId]) ->toString(), ]), 'warning'); $random = new Random(); $results = [ Loading @@ -407,34 +420,89 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface 'cx' => $options['query']['cx'], ], ], 'context' => [ 'facets' => [], ], 'context' => [], 'items' => [], ]; $results['searchInformation'] = [ 'totalResults' => $results['queries']['request']['totalResults'], ]; // Facets. for ($i = 0; $i < rand(0, 10); $i++) { $results['context']['facets'][][] = [ 'label' => $random->string(), 'anchor' => $random->string(), 'label_with_op' => "more:{$random->string()}", // Items. for ($i = 0; $i < $options['query']['num']; $i++) { $title = $random->sentences(1); $link = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(); $snippet = $random->paragraphs(1); $article = (object) [ 'name' => $title, 'description' => $snippet, ]; $pagemap = new \stdClass(); if (rand(0, 1)) { $image = ''; $directory = "public://gss"; if (file_prepare_directory($directory, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY)) { $image = $random->image("public://gss/gss_{$i}.jpg", '240x160', '240x160'); } // Items. for ($i = 0; $i < $options['query']['num']; $i++) { $pagemap->cse_thumbnail = [ (object) [ 'width' => 240, 'height' => 160, 'src' => $image, ], ]; $pagemap->cse_image = [ (object) [ 'src' => $image, ], ]; $article->image = $image; } $pagemap->article = [$article]; $results['items'][$i] = [ 'kind' => 'customsearch#result', 'title' => $random->sentences(1), 'link' => Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), 'display_link' => Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), 'snippet' => $random->paragraphs(1), 'title' => $title, 'htmlTitle' => Html::escape($title), 'link' => $link, 'display_link' => $link, 'snippet' => $snippet, 'htmlSnippet' => Html::escape($snippet), 'cacheId' => $random->string(), 'formattedUrl' => $link, 'htmlFormatterUrl' => Html::escape($link), 'pagemap' => $pagemap, ]; } // Labels. if ($this->configuration['labels']) { $results['context']['facets'] = []; $labels = []; for ($i = 0; $i < rand(2, 4); $i++) { $label = $random->string(); if (!isset($labels[$label])) { $results['context']['facets'][][] = [ 'label' => $label, 'anchor' => $label, 'label_with_op' => "more:{$label}", ]; $results['items'][$i]['htmlSnippet'] = Html::escape($results['items'][$i]['snippet']); } $labels[$label] = TRUE; } $query = $this->getParameters(); if (isset($query['label']) && !isset($labels[$query['label']])) { $results['context']['facets'][][] = [ 'label' => $query['label'], 'anchor' => $query['label'], 'label_with_op' => "more:{$query['label']}", ]; } } return json_encode($results); Loading @@ -452,12 +520,14 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface protected function prepareResults(array $items) { $results = []; foreach ($items as $item) { $extra = $this->moduleHandler->invokeAll('gss_search_result', [$item]); $results[] = [ 'link' => $item->link, 'type' => NULL, 'title' => $item->title, 'node' => NULL, 'extra' => NULL, 'extra' => $extra, 'score' => NULL, 'snippet' => [ '#markup' => $item->htmlSnippet, Loading Loading
gss.api.php 0 → 100644 +44 −0 Changes for gss.api.php: 44 added lines, 0 removed lines. Original line number Diff line number Diff line <?php /** * @file * Hooks specific to the Google Site Search module. */ /** * @addtogroup hooks * @{ */ /** * Act on a GSS search result being displayed. * * This hook is invoked from the gss search plugin during search execution, * after retrieving the search results. * * @param stdClass $result * The search result item from Google Site Search API. * * @return array * Extra information to be displayed with search result. This information * should be presented as an associative array. It will be concatenated with * the post information (last updated, author) in the default search result * theming. * * @see template_preprocess_search_result() * @see search-result.html.twig */ function hook_gss_search_result($result) { if (isset($result->pagemap->cse_thumbnail[0])) { return [ 'cse_thumbnail' => [ '#theme' => 'image', '#uri' => $result->pagemap->cse_thumbnail[0]->src, ], ]; } } /** * @} End of "addtogroup hooks". */
gss.module +14 −0 Changes for gss.module: 14 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -23,3 +23,17 @@ function gss_help($route_name, RouteMatchInterface $route_match) { return ''; } } /** * Implements hook_gss_search_result(). */ function gss_gss_search_result($result) { if (isset($result->pagemap->cse_thumbnail[0])) { return [ 'cse_thumbnail' => [ '#theme' => 'image', '#uri' => $result->pagemap->cse_thumbnail[0]->src, ], ]; } }
src/Plugin/Search/Search.php +89 −19 Changes for src/Plugin/Search/Search.php: 89 added lines, 19 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ namespace Drupal\gss\Plugin\Search; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Random; use Drupal\Core\Access\AccessResult; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Session\AccountInterface; Loading Loading @@ -55,6 +56,13 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface */ protected $languageManager; /** * A module manager object. * * @var \Drupal\Core\Extension\ModuleHandlerInterface */ protected $moduleHandler; /** * HTTP client. * Loading @@ -78,6 +86,7 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface $plugin_id, $plugin_definition, $container->get('language_manager'), $container->get('module_handler'), $container->get('http_client'), $container->get('key.repository') ); Loading @@ -94,13 +103,16 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface * The plugin implementation definition. * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager * The language manager. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * A module manager object. * @param \GuzzleHttp\Client $http_client * The http client. * @param \Drupal\key\KeyRepository $key_repository * The key repository. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, Client $http_client, KeyRepository $key_repository) { public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, Client $http_client, KeyRepository $key_repository) { $this->languageManager = $language_manager; $this->moduleHandler = $module_handler; $this->httpClient = $http_client; $this->keyRepository = $key_repository; parent::__construct($configuration, $plugin_id, $plugin_definition); Loading Loading @@ -385,7 +397,8 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface */ protected function generateSampleItems($base_url, $options) { drupal_set_message($this->t('GSS Debug mode enabled, visit the <a href=":settings">search page settings</a> to disable.', [ ':settings' => Url::fromRoute('entity.search_page.edit_form', ['search_page' => $this->searchPageId])->toString(), ':settings' => Url::fromRoute('entity.search_page.edit_form', ['search_page' => $this->searchPageId]) ->toString(), ]), 'warning'); $random = new Random(); $results = [ Loading @@ -407,34 +420,89 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface 'cx' => $options['query']['cx'], ], ], 'context' => [ 'facets' => [], ], 'context' => [], 'items' => [], ]; $results['searchInformation'] = [ 'totalResults' => $results['queries']['request']['totalResults'], ]; // Facets. for ($i = 0; $i < rand(0, 10); $i++) { $results['context']['facets'][][] = [ 'label' => $random->string(), 'anchor' => $random->string(), 'label_with_op' => "more:{$random->string()}", // Items. for ($i = 0; $i < $options['query']['num']; $i++) { $title = $random->sentences(1); $link = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(); $snippet = $random->paragraphs(1); $article = (object) [ 'name' => $title, 'description' => $snippet, ]; $pagemap = new \stdClass(); if (rand(0, 1)) { $image = ''; $directory = "public://gss"; if (file_prepare_directory($directory, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY)) { $image = $random->image("public://gss/gss_{$i}.jpg", '240x160', '240x160'); } // Items. for ($i = 0; $i < $options['query']['num']; $i++) { $pagemap->cse_thumbnail = [ (object) [ 'width' => 240, 'height' => 160, 'src' => $image, ], ]; $pagemap->cse_image = [ (object) [ 'src' => $image, ], ]; $article->image = $image; } $pagemap->article = [$article]; $results['items'][$i] = [ 'kind' => 'customsearch#result', 'title' => $random->sentences(1), 'link' => Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), 'display_link' => Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), 'snippet' => $random->paragraphs(1), 'title' => $title, 'htmlTitle' => Html::escape($title), 'link' => $link, 'display_link' => $link, 'snippet' => $snippet, 'htmlSnippet' => Html::escape($snippet), 'cacheId' => $random->string(), 'formattedUrl' => $link, 'htmlFormatterUrl' => Html::escape($link), 'pagemap' => $pagemap, ]; } // Labels. if ($this->configuration['labels']) { $results['context']['facets'] = []; $labels = []; for ($i = 0; $i < rand(2, 4); $i++) { $label = $random->string(); if (!isset($labels[$label])) { $results['context']['facets'][][] = [ 'label' => $label, 'anchor' => $label, 'label_with_op' => "more:{$label}", ]; $results['items'][$i]['htmlSnippet'] = Html::escape($results['items'][$i]['snippet']); } $labels[$label] = TRUE; } $query = $this->getParameters(); if (isset($query['label']) && !isset($labels[$query['label']])) { $results['context']['facets'][][] = [ 'label' => $query['label'], 'anchor' => $query['label'], 'label_with_op' => "more:{$query['label']}", ]; } } return json_encode($results); Loading @@ -452,12 +520,14 @@ class Search extends ConfigurableSearchPluginBase implements AccessibleInterface protected function prepareResults(array $items) { $results = []; foreach ($items as $item) { $extra = $this->moduleHandler->invokeAll('gss_search_result', [$item]); $results[] = [ 'link' => $item->link, 'type' => NULL, 'title' => $item->title, 'node' => NULL, 'extra' => NULL, 'extra' => $extra, 'score' => NULL, 'snippet' => [ '#markup' => $item->htmlSnippet, Loading