Articles


Showing Views Better Exposed Filter Results Based on User Roles in Drupal with "Input Required" Setting in Drupal 8/9/10 views.

Showing Views Better Exposed Filter Results Based on User Roles in Drupal with "Input Required" Setting in Drupal 8/9/10 views.



Posted byGokul Jayan,21st Jun 2024

 

In certain scenarios, you may want to display filtered results in Drupal based on the roles of your users. This ensures that different users have a customized experience that aligns with their role's requirements and permissions. Here’s a comprehensive guide on how you can achieve this using Better Exposed Filters in Drupal. Understanding the Requirements

There are situations where you might want different filtering behaviours for different user roles. For instance:

Anonymous Users: Only show results when an anonymous user adds a filter. Administrators: Display all results for administrators without requiring any filter.

Implementing Role-Based Filtering

Since the "Input required" field within the views has a common setting and does not support role-based configurations out-of-the-box, we need to implement custom solutions using Drupal hooks.

Step 1: Enable "Input Required" settings

  • Go to the view where you want to apply the filter.
  • Edit the exposed filter settings.
  • Enable the "Input required" checkbox in the Better Exposed Filters settings. This ensures that anonymous users must add a filter value to see any results.

Step 2: Use Hooks for Administrator Behavior

Since the "Input required" field within the views has a common setting and does not support role-based configurations by default, you need to add custom code to your Drupal site. Here’s how to do it:

  • Create a Custom Module (if not already done):
  • Create a directory for your custom module, e.g., custom/my_custom_module.
  • Create the module info file, my_custom_module.info.yml:
name: 'My Custom Module'
type: module
description: 'Custom module to alter BEF according to user roles.'
core_version_requirement: ^8 || ^9 || ^10
package: Custom
dependencies:
  - views
  - better_exposed_filters
  • Implement the Custom Hooks: Create the module file, my_custom_module.module:
<?php

use Drupal\views\ViewExecutable;
use Drupal\user\Entity\User;

/**
 * Implements hook_views_pre_view().
 */
function my_custom_module_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  if (($view->id() === 'VIEW_ID') && ($display_id === 'VIEW_DISPLAY_ID')) {
    $account = User::load(\Drupal::currentUser()->id());
    $roles = $account->getRoles();
    if (in_array('administrator', $roles)) {
      $view->display_handler->getPlugin('exposed_form')->options['bef']['general']['input_required'] = FALSE;
    }
  }
}

/**
 * Implements hook_better_exposed_filters_options_alter().
 */
function my_custom_module_better_exposed_filters_options_alter(array &$options, ViewExecutable $view, \Drupal\views\Plugin\views\display\DisplayPluginBase $displayHandler) {
  if (($view->id() === 'VIEW_ID') && ($display_id === 'VIEW_DISPLAY_ID')) {
    $account = User::load(\Drupal::currentUser()->id());
    $roles = $account->getRoles();
    if (in_array('administrator', $roles)) {
      $view->display_handler->getPlugin('exposed_form')->options['bef']['general']['input_required'] = FALSE;
    }
  }
} 

Summary

By following these steps, you ensure that:

Anonymous users see results only when they apply a filter.
Administrators can see all results without needing to apply any filters.

This approach leverages Better Exposed Filters settings and custom hooks to tailor the view results based on user roles, enhancing the user experience on your Drupal site.