Block Exclude Pages

Adds exclude pages filter functionality to block visibility settings using the '!' prefix notation.

block_exclude_pages
7,708 sites
51
drupal.org

Install

Drupal 11, 10 v2.2.0
composer require 'drupal/block_exclude_pages:^2.2'

Overview

Block Exclude Pages enhances Drupal's native block visibility system by allowing site administrators to exclude specific pages from wildcard patterns. This solves a common problem where you want to show a block on most pages matching a pattern but need to hide it on specific pages within that pattern.

The module works by intercepting the block visibility evaluation process and processing paths prefixed with '!' as exclusions. When visibility is set to 'show for listed pages', excluded paths will hide the block despite matching wildcards. Conversely, when set to 'hide for listed pages', excluded paths will show the block despite matching the hide pattern.

This lightweight module requires no configuration pages and seamlessly integrates with Drupal's existing block configuration interface by adding help text and modifying the visibility condition behavior.

Features

  • Exclude specific pages from block visibility wildcards using '!' prefix notation
  • Works with both 'show for listed pages' and 'hide for listed pages' visibility modes
  • Supports path aliasing - matches both internal paths and URL aliases
  • Language-aware path handling - automatically handles language prefixes in URLs
  • Extends Drupal's core RequestPath condition plugin with enhanced validation
  • Seamlessly integrates with existing block configuration interface without additional admin pages
  • Supports complex exclusion patterns including nested wildcards (e.g., !/user/jc/*)

Use Cases

Show sidebar block on all user pages except specific user profiles

You want to display a promotional sidebar block on all user-related pages (/user/*) but hide it on administrator profiles. Configure the block visibility with: /user/* to show on all user pages, then add !/user/1 and !/user/admin to exclude the admin user pages.

Display content block on section pages with exceptions

A corporate site has an HR section at /offices/human-resources/* but needs to hide certain blocks on manager-only pages. Set visibility to /offices/human-resources/current-employees/* then exclude with !/offices/human-resources/current-employees/manager-documents and !/offices/human-resources/current-employees/manager-resources.

Hide advertising block everywhere except on specific promotional pages

When using 'hide for listed pages' mode with /* to hide a block everywhere, you can use !/ prefix patterns to create exceptions where the block should appear. For example, hide on /* but show on !/promotions and !/special-offers.

Language-neutral block visibility with exclusions

On multilingual sites, the module automatically strips language prefixes from paths before matching. Configure /products/* with !/products/discontinued and it will work correctly for both /en/products/discontinued and /es/products/discontinued.

Tips

  • Always use a leading forward slash for paths (e.g., /user/profile, not user/profile)
  • Exclude patterns must start with '!' followed by the path (e.g., !/user/jc)
  • Wildcards work in exclude patterns too - use !/user/jc/* to exclude all pages under a path
  • The <front> token can also be excluded using !<front>
  • Place more specific exclude patterns after the wildcard patterns in your configuration for clarity
  • Test your visibility rules by viewing the block on different pages after configuration

Technical Details

Hooks 4
hook_help

Provides help page content describing how to use the exclude paths feature

hook_form_block_form_alter

Modifies the block configuration form to add help text about exclude path syntax to the pages visibility field

hook_block_access

Core hook that evaluates block visibility and processes exclude patterns. Checks if current path matches any exclude pattern and inverts the visibility condition accordingly

hook_condition_info_alter

Replaces the core RequestPath condition plugin class with the module's extended version that supports '!' prefix validation