Commit ee7eb60c authored by webchick's avatar webchick

Issue #146278 by jhodgdon, pwolanin, douggreen: The "No results" text is not...

Issue #146278 by jhodgdon, pwolanin, douggreen: The "No results" text is not correct for UserSearch -- search help needs to be per-plugin, not global
parent 752b99ff
......@@ -109,17 +109,11 @@ public function view(Request $request, SearchPageInterface $entity) {
);
}
$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>&quot;bike shed&quot;</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(
// @todo Revisit where this help text is added.
'#markup' => '<h3>' . $this->t('Your search yielded no results.') . '</h3>' . $no_results,
'#markup' => '<h3>' . $this->t('Your search yielded no results.') . '</h3>',
),
'#list_type' => 'ol',
'#attributes' => array(
......@@ -142,6 +136,25 @@ public function view(Request $request, SearchPageInterface $entity) {
return $build;
}
/**
* 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;
}
/**
* Redirects to a search page.
*
......
......@@ -9,6 +9,7 @@
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Provides a search form for site wide search.
......@@ -55,6 +56,7 @@ public function form(array $form, FormStateInterface $form_state) {
'#size' => 30,
'#maxlength' => 255,
);
// processed_keys is used to coordinate keyword passing between other forms
// that hook into the basic search form.
$form['basic']['processed_keys'] = array(
......@@ -66,6 +68,13 @@ public function form(array $form, FormStateInterface $form_state) {
'#value' => $this->t('Search'),
);
$form['help_link'] = array(
'#type' => 'link',
'#url' => new Url('search.help_' . $this->entity->id()),
'#title' => $this->t('Search help'),
'#options' => array('attributes' => array('class' => 'search-help-link')),
);
// Allow the plugin to add to or alter the search form.
$plugin->searchFormAlter($form, $form_state);
......
......@@ -90,6 +90,14 @@ public function buildResults();
*/
public function suggestedTitle();
/**
* Returns the searching help.
*
* @return array
* Render array for the searching help.
*/
public function getHelp();
/**
* Alters the search form when being built for a given plugin.
*
......
......@@ -133,4 +133,25 @@ public function buildSearchUrlQuery(FormStateInterface $form_state) {
return $query;
}
/*
* {@inheritdoc}
*/
public function getHelp() {
// This default search help is appropriate for plugins like NodeSearch
// that use the SearchQuery class.
$help = array('list' => array(
'#theme' => 'item_list',
'#items' => array(
$this->t('Search looks for exact, case-insensitive keywords; keywords shorter than a minimum length are ignored.'),
$this->t('Use upper-case OR to get more results. Example: cat OR dog (content contains either "cat" or "dog").'),
$this->t('You can use upper-case AND to require all words, but this is the same as the default behavior. Example: cat AND dog (same as cat dog, content must contain both "cat" and "dog").'),
$this->t('Use quotes to search for a phrase. Example: "the cat eats mice".'),
$this->t('You can precede keywords by - to exclude them; you must still have at least one "positive" keyword. Example: cat -dog (content must contain cat and cannot contain dog).'),
),
));
return $help;
}
}
......@@ -95,6 +95,26 @@ public function routes() {
),
)
);
$routes["search.help_$entity_id"] = new Route(
'/search/' . $entity->getPath() . '/help',
array(
'_controller' => 'Drupal\search\Controller\SearchController::searchHelp',
'_title' => 'Search help',
'entity' => $entity_id,
),
array(
'_entity_access' => 'entity.view',
'_permission' => 'search content',
),
array(
'parameters' => array(
'entity' => array(
'type' => 'entity:search_page',
),
),
)
);
}
return $routes;
}
......
......@@ -68,6 +68,6 @@ function testNodeType() {
$this->assertText($this->node->label(), 'Basic page node is found with POST query and type:page.');
$this->drupalPostForm('search/node', array_merge($edit, array('type[article]' => 'article')), t('Advanced search'));
$this->assertText('bike shed', 'Article node is not found with POST query and type:article.');
$this->assertText('search yielded no results', 'Article node is not found with POST query and type:article.');
}
}
......@@ -11,7 +11,7 @@
use Drupal\Component\Utility\Unicode;
/**
* Tests the bike shed text on no results page, and text on the search page.
* Tests the search help text and search page text.
*
* @group search
*/
......@@ -44,7 +44,7 @@ function testSearchText() {
$search_terms = 'bike shed ' . $this->randomMachineName();
$edit['keys'] = $search_terms;
$this->drupalPostForm('search/node', $edit, t('Search'));
$this->assertText(t('Consider loosening your query with OR. bike OR shed will often show more results than bike shed.'), 'Help text is displayed when search returns no results.');
$this->assertText('search yielded no results');
$this->assertText(t('Search'));
$title_source = 'Search for @keywords | Drupal';
$this->assertTitle(t($title_source, array('@keywords' => Unicode::truncate($search_terms, 60, TRUE, TRUE))), 'Search page title is correct');
......@@ -52,6 +52,10 @@ function testSearchText() {
$this->assertNoText(t('Node'), 'Erroneous translated tab and breadcrumb text is not present');
$this->assertText(t('Content'), 'Tab and breadcrumb text is present');
$this->clickLink('Search help');
$this->assertText('Search help', 'Correct title is on search help page');
$this->assertText('Use upper-case OR to get more results', 'Correct text is on content search help page');
// Search for a longer text, and see that it is in the title, truncated.
$edit = array();
$search_terms = 'Every word is like an unnecessary stain on silence and nothingness.';
......@@ -71,6 +75,10 @@ function testSearchText() {
$this->assertText(t('Search'));
$this->assertTitle(t($title_source, array('@keywords' => Unicode::truncate($this->searchingUser->getUsername(), 60, TRUE, TRUE))));
$this->clickLink('Search help');
$this->assertText('Search help', 'Correct title is on search help page');
$this->assertText('user names and partial user names', 'Correct text is on user search help page');
// Test that search keywords containing slashes are correctly loaded
// from the GET params and displayed in the search form.
$arg = $this->randomMachineName() . '/' . $this->randomMachineName();
......
......@@ -160,4 +160,19 @@ public function execute() {
return $results;
}
/*
* {@inheritdoc}
*/
public function getHelp() {
$help = array('list' => array(
'#theme' => 'item_list',
'#items' => array(
$this->t('User search looks for user names and partial user names. Example: mar would match usernames mar, delmar, and maryjane.'),
$this->t('You can use * as a wildcard within your keyword. Example: m*r would match user names mar, delmar, and elementary.'),
),
));
return $help;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment