You are here

protected function Search::generateSampleItems in Google Site Search 8

Returns dummy search results for debug mode.

Return value

string JSON data for debug mode.

1 call to Search::generateSampleItems()
Search::getResults in src/Plugin/Search/Search.php
Get query result.

File

src/Plugin/Search/Search.php, line 466

Class

Search
Handles search using Google Search Engine.

Namespace

Drupal\gss\Plugin\Search

Code

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(),
  ]), 'warning');
  $random = new Random();
  $results = [
    'kind' => 'customsearch#search',
    'url' => [
      'type' => 'application/json',
      'template' => 'https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json',
    ],
    'queries' => [
      'request' => [
        'title' => "Google Custom Seearch - {$options['query']['q']}",
        'totalResults' => rand($options['query']['start'], 10000),
        'searchTerms' => $options['query']['q'],
        'count' => $options['query']['num'],
        'startIndex' => $options['query']['start'],
        'inputEncoding' => 'utf8',
        'outputEncoding' => 'utf8',
        'safe' => 'off',
        'cx' => $options['query']['cx'],
      ],
    ],
    'context' => [],
    'items' => [],
  ];
  $results['searchInformation'] = [
    'totalResults' => $results['queries']['request']['totalResults'],
  ];

  // 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');
      }
      $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' => $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}",
        ];
      }
      $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);
}