Skip to content
Snippets Groups Projects
Commit c83ff69e authored by Angie Byron's avatar Angie Byron
Browse files

Issue #1926344 by jhodgdon, maggo, mdrummond, Cottser, dimboz | jenlampton:...

Issue #1926344 by jhodgdon, maggo, mdrummond, Cottser, dimboz | jenlampton: Consolidate search-result.html.twig and search-results.html.twig.
parent c87ff104
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -68,8 +68,7 @@ public function view(Request $request, SearchPageInterface $entity, $keys = '')
$plugin = $entity->getPlugin();
$plugin->setSearch($keys, $request->query->all(), $request->attributes->all());
// Default results output is an empty string.
$results = array('#markup' => '');
$results = array();
// Process the search form. Note that if there is
// \Drupal::request()->request data, search_form_submit() will cause a
......@@ -88,7 +87,32 @@ public function view(Request $request, SearchPageInterface $entity, $keys = '')
}
// The form may be altered based on whether the search was run.
$build['search_form'] = $this->entityManager()->getForm($entity, 'search');
$build['search_results'] = $results;
if (count($results)) {
$build['search_results_title'] = array(
'#markup' => '<h2>' . $this->t('Search results') . '</h2>',
);
}
$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>' . search_help('search#noresults', drupal_help_arg()),
),
'#list_type' => 'ol',
'#attributes' => array(
'class' => array(
'search-results',
$plugin->getPluginId() . '-results',
),
),
);
$build['pager'] = array(
'#theme' => 'pager',
);
return $build;
}
......
......@@ -72,10 +72,12 @@ public function isSearchExecutable();
public function execute();
/**
* Executes the search and builds a render array.
* Executes the search and builds render arrays for the result items.
*
* @return array
* The search results in a renderable array.
* An array of render arrays of search result items (generally each item
* has '#theme' set to 'search_result'), or an empty array if there are no
* results.
*/
public function buildResults();
......
......@@ -88,11 +88,17 @@ public function isSearchExecutable() {
*/
public function buildResults() {
$results = $this->execute();
return array(
'#theme' => 'search_results',
'#results' => $results,
'#plugin_id' => $this->getPluginId(),
);
$built = array();
foreach ($results as $result) {
$built[] = array(
'#theme' => 'search_result',
'#result' => $result,
'#plugin_id' => $this->getPluginId(),
);
}
return $built;
}
/**
......
......@@ -108,11 +108,6 @@ function search_theme() {
'file' => 'search.pages.inc',
'template' => 'search-result',
),
'search_results' => array(
'variables' => array('results' => NULL, 'plugin_id' => NULL),
'file' => 'search.pages.inc',
'template' => 'search-results',
),
);
}
......
......@@ -6,35 +6,6 @@
*/
use Drupal\Core\Language\Language;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
* Prepares variables for search results templates.
*
* Default template: search-results.html.twig.
*
* @param array $variables
* An array with the following elements:
* - results: Search results array.
* - plugin_id: Plugin the search results came from.
*/
function template_preprocess_search_results(&$variables) {
$variables['search_results'] = '';
if (!empty($variables['plugin_id'])) {
$variables['plugin_id'] = check_plain($variables['plugin_id']);
}
foreach ($variables['results'] as $result) {
$variables['search_results'][] = array(
'#theme' => 'search_result',
'#result' => $result,
'#plugin_id' => $variables['plugin_id'],
);
}
$variables['pager'] = array('#theme' => 'pager');
// @todo Revisit where this help text is added, see also
// http://drupal.org/node/1918856.
$variables['help'] = search_help('search#noresults', drupal_help_arg());
}
/**
* Implements hook_theme_suggestions_HOOK().
......
......@@ -3,9 +3,10 @@
* @file
* Default theme implementation for displaying a single search result.
*
* This template renders a single search result and is collected into
* search-results.html.twig. This and the parent template are
* dependent to one another sharing the markup for ordered lists.
* This template renders a single search result. The list of results is
* rendered using '#theme' => 'item_list', with suggestions of:
* - item_list__search_results__(plugin_id)
* - item_list__search_results
*
* Available variables:
* - url: URL of the result.
......@@ -57,18 +58,16 @@
* @ingroup themeable
*/
#}
<li {{ attributes }}>
{{ title_prefix }}
<h3 class="title"{{ title_attributes }}>
<a href="{{ url }}">{{ title }}</a>
</h3>
{{ title_suffix }}
<div class="search-snippet-info">
{% if snippet %}
<p class="search-snippet"{{ content_attributes }}>{{ snippet }}</p>
{% endif %}
{% if info %}
<p class="search-info">{{ info }}</p>
{% endif %}
</div>
</li>
{{ title_prefix }}
<h3 class="title"{{ title_attributes }}>
<a href="{{ url }}">{{ title }}</a>
</h3>
{{ title_suffix }}
<div class="search-snippet-info">
{% if snippet %}
<p class="search-snippet"{{ content_attributes }}>{{ snippet }}</p>
{% endif %}
{% if info %}
<p class="search-info">{{ info }}</p>
{% endif %}
</div>
{#
/**
* @file
* Default theme implementation for displaying search results.
*
* This template collects each invocation of theme_search_result(). This and
* the child template are dependent to one another sharing the markup for
* definition lists.
*
* Note that modules may implement their own search type and theme function
* completely bypassing this template.
*
* Available variables:
* - search_results: All results as it is rendered through
* search-result.html.twig.
* - plugin_id: The machine-readable name of the plugin being executed, such
* as 'node_search' or 'user_search'.
* - pager: The pager next/prev links to display, if any.
* - help: HTML for help text to display when no results are found.
*
* @see template_preprocess_search_results()
*
* @ingroup themeable
*/
#}
{% if search_results %}
<h2>{{ 'Search results'|t }}</h2>
<ol class="search-results {{ plugin_id }}-results">
{{ search_results }}
</ol>
{{ pager }}
{% else %}
<h2>{{ 'Your search yielded no results'|t }}</h2>
{{ help }}
{% endif %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment