Drupal 8: Creating A Views Results Area Plugin

23rd March 2020

Drupal Views is a great way of exposing data to users via a search interface. This can be done directly via the database or via a Solr server. Plenty of Views plugins exist to allow reacting to the search input and output in a variety of different ways.

The other day I needed to add a personalised message to Views output to inform a user that their search keyword didn't find any results. There is a plugin for Views that allows this, but it only shows a basic search string.

What I needed was a way to print out the following block of HTML, containing the search term that the user searched for.

<p class="no-results-found">Your search for <span>"monkey"</span> didn't return any results.</p>

What was needed here was a Views Area Plugin. Using this plugin type I could include this HTML into the page when the View reported that there was no results. To this end I created a Views area plugin called 'NoResultsFound' at the location src/Plugin/views/area in a custom module.

This is the class I created, which extends the class Drupal\views\Plugin\views\area\AreaPluginBase. The render() function received an input from Views called $empty, which is true if the View has no results. Note that this implementation is slightly tied into the View I was using as it relies on a 'query' parameter, which was the keyword field from the View I was using.

  1. <?php
  2.  
  3. namespace Drupal\custom_search\Plugin\views\area;
  4.  
  5. use Drupal\views\Plugin\views\area\AreaPluginBase;
  6.  
  7. /**
  8.  * Defines a views area plugin.
  9.  *
  10.  * @ingroup views_area_handlers
  11.  *
  12.  * @ViewsArea("no_results_found")
  13.  */
  14. class NoResultsFound extends AreaPluginBase {
  15.  
  16. /**
  17.   * {@inheritdoc}
  18.   */
  19. public function render($empty = FALSE) {
  20. if ($empty) {
  21. $query = $this->view->getRequest()->query->get('query');
  22.  
  23. $message = $this->t('<p class="no-results-found">Your search for <span>"@query"</span> didn\'t return any results.</p>', ['@query' => $query]);
  24.  
  25. return [
  26. '#markup' => $message,
  27. ];
  28. }
  29.  
  30. return [];
  31. }
  32.  
  33. }

All that was needed now was to tell Views that the class existed. To do this I needed to add an implementation of the hook hook_views_data() to a file called custom_search_search.views.inc. This informs Views about the existence of the plugin.

  1. <?php
  2.  
  3. /**
  4.  * Implements hook_views_data().
  5.  */
  6. function custom_search_views_data() {
  7. $data['views']['no_results_found'] = [
  8. 'title' => t('No results found'),
  9. 'help' => t('Print out a query not found message.'),
  10. 'area' => [
  11. 'id' => 'no_results_found',
  12. ],
  13. ];
  14.  
  15. return $data;
  16. }

All that is needed now is to add the plugin to the View configuration so that when a user finds no results we can inform them of the problem.

Add new comment

The content of this field is kept private and will not be shown publicly.