You are here

private function SearchStyleguide::searchResults in Style Guide 8

Same name and namespace in other branches
  1. 2.x src/Plugin/Styleguide/SearchStyleguide.php \Drupal\styleguide\Plugin\Styleguide\SearchStyleguide::searchResults()

Generate fake search results.

Parameters

array $items: An array of the search Styleguide elements.

1 call to SearchStyleguide::searchResults()
SearchStyleguide::items in src/Plugin/Styleguide/SearchStyleguide.php
Styleguide elements implementation.

File

src/Plugin/Styleguide/SearchStyleguide.php, line 169

Class

SearchStyleguide
Search Styleguide items implementation.

Namespace

Drupal\styleguide\Plugin\Styleguide

Code

private function searchResults(array &$items) {
  $results = [];
  if (!empty($this->searchManager)) {
    $definitions = $this->searchManager
      ->getDefinitions();

    // If definitions has "user_search" provider,
    // we should show user for admin users.
    if (in_array('user_search', array_keys($definitions))) {
      $definitions['user_search_admin'] = [
        'id' => 'user_search_admin',
        'provider' => 'user_admin',
      ];
    }
    foreach ($definitions as $definition) {
      $search_provider = $definition['provider'];

      // Generate fake search results.
      for ($i = 0; $i < 5; $i++) {
        if ($search_provider == 'node') {
          $title = $this
            ->t('Node Search, results');
          $result = $this
            ->searchNodePrepare($i);
        }
        else {
          if ($search_provider == 'user_admin') {
            $title = $this
              ->t('User Search, results (Has permission "administer users")');
            $result = [
              'title' => $this->generator
                ->words(1) . " (user_{$i}@email.com)",
            ];
          }
          else {
            $title = $this
              ->t('@type Search, results', [
              '@type' => ucfirst($search_provider),
            ]);
            $result = [
              'title' => $this->generator
                ->words(1),
            ];
          }
        }
        $results[$i] = [
          '#theme' => 'search_result',
          '#result' => [
            'link' => '#',
          ],
          '#plugin_id' => $definition['id'],
        ];
        $results[$i]['#result'] = array_merge($results[$i]['#result'], $result);
      }

      // Attach fake search results to the items.
      $items["{$definition['id']}_search_results"] = [
        'title' => $title,
        'content' => [
          '#theme' => 'item_list__search_results',
          '#items' => $results,
          '#empty' => [
            '#markup' => '<h3>' . $this
              ->t('Your search yielded no results.') . '</h3>',
          ],
          '#list_type' => 'ol',
          '#context' => [
            'plugin' => $definition['id'],
          ],
        ],
        'group' => $this
          ->t('Search'),
      ];
    }
  }
}