3

Is there any function equivalent to the (D7) search_data() in Drupal 8? I would like to get the search results as a renderable array or html for a given keyword programatically.

In D8, the SearchController.php provides the view which does the same, but I'm not sure how to call that from my module?

It uses

$plugin = $entity->getPlugin();
$plugin->setSearch();
$plugin->buildResults();

to get the result.

How do I create the SearchPageInterface entity which contain the getPlugin() method? Or is there a way to create an instance of the default search plugin directly?

My requirement is to show the search results in a custom page where the search results are wrapped by the custom page content. I've implemented this in D7 using search_data function. How can I achieve similar result in D8?

2

You don't create the search page entity, you load it. SearchPage::load($id). Either hardcode the id that you use on your site or make it configurable through configuration/settings/URL.

|improve this answer|||||
  • Thanks for the tip. I will try to get the default search page using SearchPageRepository::getDefaultSearchPage() and load it with SearchPage::load($id). Will update the results here. – Vimal Dec 1 '14 at 18:43
  • Thanks, the SearchPage::load($id) is working. I'm not able to figure out the right method to get the default search page. SearchPageRepository::getDefaultSearchPage() is not working. The getDefaultSearchPage execute a query with $this->getQuery() and the getQuery() returns the query object from $entity_manager->getStorage('search_page'); which is set by the SearchPageRepository constructor. I am not able to figure out how to load the $entity_manager so that I can call getStorage('search_page'); – Vimal Dec 2 '14 at 6:57
  • That is a service. See search.services.yml on how it is defined. You don't need to create the object yourself, just access that service, with \Drupal::service('search.search_page_repository') or by injecting it into your controller. – Berdir Dec 2 '14 at 17:48
0

You could try hook_search_page to Override the rendering of search results.

You could also look into search_data API.

|improve this answer|||||
  • This will not work. 1) The search_data() is not there in D8. 2) I do not want to override the search results, but use it with in my custom menu. – Vimal Dec 1 '14 at 18:39
0

Maybe, by example:

$query = \Drupal::entityQuery('node')
->condition('type', 'article')
->condition('title', 'cat', 'CONTAINS')
$nids = $query->execute();

Return a list of node ids where the field "title" contain the word "cat". After you can use:

$nodes = entity_load_multiple('node', $nids);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.