protected function Search::getResults in Google Site Search 8
Get query result.
Parameters
int $n: Number of items.
int $offset: Offset of items (0-indexed).
string $search_type: One of:
- NULL (regular search).
- "image".
Return value
object|null Decoded response from Google, or NULL on error.
1 call to Search::getResults()
- Search::findResults in src/
Plugin/ Search/ Search.php - Queries to find search results, and sets status messages.
File
- src/
Plugin/ Search/ Search.php, line 388
Class
- Search
- Handles search using Google Search Engine.
Namespace
Drupal\gss\Plugin\SearchCode
protected function getResults($n = 1, $offset = 0, $search_type = NULL) {
$language = $this->languageManager
->getCurrentLanguage()
->getId();
$language_default = $this->languageManager
->getDefaultLanguage()
->getId();
$any_language = LanguageInterface::LANGCODE_NOT_SPECIFIED;
$api_key = $this->keyRepository
->getKey($this->configuration['api_key']);
$query = $this
->getParameters();
$search_engine_id = NULL;
if (!empty($this->configuration['search_engine_id_' . $language])) {
// Language specific search engine id.
$search_engine_id = $this->configuration['search_engine_id_' . $language];
}
elseif (!empty($this->configuration['search_engine_id_' . $language_default])) {
// Default language search engine id.
$search_engine_id = $this->configuration['search_engine_id_' . $language_default];
$language = $language_default;
}
elseif (!empty($this->configuration['search_engine_id_' . $any_language])) {
// Any language search engine id.
$search_engine_id = $this->configuration['search_engine_id_' . $any_language];
}
// Exit, no search engine id defined.
if (empty($search_engine_id)) {
return NULL;
}
// make sure we actually have a base url
$base_url = $this->configuration['base_url'];
if (empty($base_url)) {
$default_config = $this
->defaultConfiguration();
$base_url = $default_config['base_url'];
}
$keywords = $this
->getKeywords();
if (isset($query['label'])) {
$keywords .= '+more:' . $query['label'];
}
$options = array(
'query' => array(
'q' => $keywords,
'key' => !is_null($api_key) ? $api_key
->getKeyValue() : NULL,
'cx' => $search_engine_id,
// hl: "interface language", also used to weight results.
'hl' => $language,
// lr: "language restrict", supposed to limit results to only the set
// language, defined with a "lang_" prefix.
'lr' => 'lang_' . $language,
'start' => $offset + 1,
'num' => $n,
),
);
if (@$query['type'] == 'image') {
$options['query']['searchType'] = 'image';
}
if (!is_null($api_key)) {
try {
$response = $this->httpClient
->get($base_url, $options);
$json = $response
->getBody();
} catch (\Exception $e) {
\Drupal::logger('gss')
->error($e
->getMessage());
return NULL;
}
}
else {
$json = $this
->generateSampleItems($base_url, $options);
}
return json_decode($json);
}