Drupal 8: Creating A Views Results Area Plugin

23rd March 2020 - 3 minutes read time

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.

<?php

namespace Drupal\custom_search\Plugin\views\area;

use Drupal\views\Plugin\views\area\AreaPluginBase;

/**
 * Defines a views area plugin.
 *
 * @ingroup views_area_handlers
 *
 * @ViewsArea("no_results_found")
 */
class NoResultsFound extends AreaPluginBase {

  /**
   * {@inheritdoc}
   */
  public function render($empty = FALSE) {
    if ($empty) {
      $query = $this->view->getRequest()->query->get('query');

      $message = $this->t('<p class="no-results-found">Your search for <span>"@query"</span> didn\'t return any results.</p>', ['@query' => $query]);

      return [
        '#markup' => $message,
      ];
    }

    return [];
  }

}

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.

<?php

/**
 * Implements hook_views_data().
 */
function custom_search_views_data() {
  $data['views']['no_results_found'] = [
    'title' => t('No results found'),
    'help' => t('Print out a query not found message.'),
    'area' => [
      'id' => 'no_results_found',
    ],
  ];

  return $data;
}

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.

Comments

Permalink

Hi Philip, thanks for this article it was very useful for me.

However I had to change hook_views_data() to hook_views_data_alter(array &$data) in a module_name_views.inc file to make it works.

Diao Diallo (Wed, 05/19/2021 - 10:03)

Add new comment

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