/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Redirect to the search page with keywords in the GET parameters. // Plugins with additional search parameters will need to provide their // own form submit handler to replace this, so they can put their values // into the GET as well. If so, make sure to put 'keys' into the GET // parameters so that the search results generation is triggered. $query = $this->entity->getPlugin()->buildSearchUrlQuery($form_state); $route = 'search.view_' . $form_state->get('search_page_id'); $form_state->setRedirect($route, array(), array('query' => $query)); }
/** * Verifies that if we combine two rankings, search still works. * * See issue http://drupal.org/node/771596 */ function testDoubleRankings() { // Login with sufficient privileges. $this->drupalLogin($this->drupalCreateUser(array('skip comment approval', 'create page content'))); // Create two nodes that will match the search, one that is sticky. $settings = array('type' => 'page', 'title' => 'Drupal rocks', 'body' => array(array('value' => "Drupal's search rocks"))); $this->drupalCreateNode($settings); $settings['sticky'] = 1; $node = $this->drupalCreateNode($settings); // Update the search index. $this->nodeSearch->getPlugin()->updateIndex(); search_update_totals(); // Set up for ranking sticky and lots of comments; make sure others are // disabled. $node_ranks = array('sticky', 'promote', 'relevance', 'recent', 'comments', 'views'); $configuration = $this->nodeSearch->getPlugin()->getConfiguration(); foreach ($node_ranks as $var) { $value = $var == 'sticky' || $var == 'comments' ? 10 : 0; $configuration['rankings'][$var] = $value; } $this->nodeSearch->getPlugin()->setConfiguration($configuration); $this->nodeSearch->save(); // Do the search and assert the results. $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array()); // Do the search and assert the results. $set = $this->nodeSearch->getPlugin()->execute(); $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search double ranking order.'); }
/** * Test rankings of HTML tags. */ public function testHTMLRankings() { $full_html_format = entity_create('filter_format', array('format' => 'full_html', 'name' => 'Full HTML')); $full_html_format->save(); // Test HTML tags with different weights. $sorted_tags = array('h1', 'h2', 'h3', 'h4', 'a', 'h5', 'h6', 'notag'); $shuffled_tags = $sorted_tags; // Shuffle tags to ensure HTML tags are ranked properly. shuffle($shuffled_tags); $settings = array('type' => 'page', 'title' => 'Simple node'); $nodes = array(); foreach ($shuffled_tags as $tag) { switch ($tag) { case 'a': $settings['body'] = array(array('value' => \Drupal::l('Drupal Rocks', new Url('<front>')), 'format' => 'full_html')); break; case 'notag': $settings['body'] = array(array('value' => 'Drupal Rocks')); break; default: $settings['body'] = array(array('value' => "<{$tag}>Drupal Rocks</{$tag}>", 'format' => 'full_html')); break; } $nodes[$tag] = $this->drupalCreateNode($settings); } // Update the search index. $this->nodeSearch->getPlugin()->updateIndex(); search_update_totals(); $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array()); // Do the search and assert the results. $set = $this->nodeSearch->getPlugin()->execute(); // Test the ranking of each tag. foreach ($sorted_tags as $tag_rank => $tag) { // Assert the results. if ($tag == 'notag') { $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for plain text order.'); } else { $this->assertEqual($set[$tag_rank]['node']->id(), $nodes[$tag]->id(), 'Search tag ranking for "<' . $sorted_tags[$tag_rank] . '>" order.'); } } // Test tags with the same weight against the sorted tags. $unsorted_tags = array('u', 'b', 'i', 'strong', 'em'); foreach ($unsorted_tags as $tag) { $settings['body'] = array(array('value' => "<{$tag}>Drupal Rocks</{$tag}>", 'format' => 'full_html')); $node = $this->drupalCreateNode($settings); // Update the search index. $this->nodeSearch->getPlugin()->updateIndex(); search_update_totals(); $this->nodeSearch->getPlugin()->setSearch('rocks', array(), array()); // Do the search and assert the results. $set = $this->nodeSearch->getPlugin()->execute(); // Ranking should always be second to last. $set = array_slice($set, -2, 1); // Assert the results. $this->assertEqual($set[0]['node']->id(), $node->id(), 'Search tag ranking for "<' . $tag . '>" order.'); // Delete node so it doesn't show up in subsequent search results. $node->delete(); } }
/** * Creates a render array for the search page. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\search\SearchPageInterface $entity * The search page entity. * * @return array * The search form and search results build array. */ public function view(Request $request, SearchPageInterface $entity) { $build = array(); $plugin = $entity->getPlugin(); // Build the form first, because it may redirect during the submit, // and we don't want to build the results based on last time's request. if ($request->query->has('keys')) { $keys = trim($request->get('keys')); $plugin->setSearch($keys, $request->query->all(), $request->attributes->all()); } $build['#title'] = $plugin->suggestedTitle(); $build['search_form'] = $this->entityFormBuilder()->getForm($entity, 'search'); // Build search results, if keywords or other search parameters are in the // GET parameters. Note that we need to try the search if 'keys' is in // there at all, vs. being empty, due to advanced search. $results = array(); if ($request->query->has('keys')) { if ($plugin->isSearchExecutable()) { // Log the search. if ($this->config('search.settings')->get('logging')) { $this->logger->notice('Searched %type for %keys.', array('%keys' => $keys, '%type' => $entity->label())); } // Collect the search results. $results = $plugin->buildResults(); } else { // The search not being executable means that no keywords or other // conditions were entered. drupal_set_message($this->t('Please enter some keywords.'), 'error'); } } if (count($results)) { $build['search_results_title'] = array('#markup' => '<h2>' . $this->t('Search results') . '</h2>'); } $no_results = t('<ul> <li>Check if your spelling is correct.</li> <li>Remove quotes around phrases to search for each word individually. <em>bike shed</em> will often show more results than <em>"bike shed"</em>.</li> <li>Consider loosening your query with <em>OR</em>. <em>bike OR shed</em> will often show more results than <em>bike shed</em>.</li> </ul>'); $build['search_results'] = array('#theme' => array('item_list__search_results__' . $plugin->getPluginId(), 'item_list__search_results'), '#items' => $results, '#empty' => array('#markup' => '<h3>' . $this->t('Your search yielded no results.') . '</h3>' . $no_results), '#list_type' => 'ol', '#attributes' => array('class' => array('search-results', $plugin->getPluginId() . '-results')), '#cache' => array('tags' => $entity->getCacheTag())); $build['pager'] = array('#theme' => 'pager'); $build['#attached']['library'][] = 'search/drupal.search.results'; return $build; }
/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $this->plugin = $this->entity->getPlugin(); return parent::buildForm($form, $form_state); }
/** * Creates a render array for the search help page. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. * @param \Drupal\search\SearchPageInterface $entity * The search page entity. * * @return array * The search help page. */ public function searchHelp(SearchPageInterface $entity) { $build = array(); $build['search_help'] = $entity->getPlugin()->getHelp(); return $build; }