Block module path visibility based on node alias rather than actual path

Created on 18 September 2011, almost 13 years ago
Updated 23 June 2023, about 1 year ago

Problem/Motivation

The block module determines path-based visibility by using a reverse lookup on the converted request querystring. This causes problems if you have content with multiple path aliases, since Drupal will look up the requested path to find the source path (e.g. /node/nid). Then, the block module takes that node ref and looks up the alias, and just takes the first one it finds. So the block module will only ever check against one of the multiple paths, leading to block visibility based on the first found node alias rather than the actual path requested.

I tried using the context module as an alternative method of setting block visibility, but it seems to do the same.

I realise that having multiple paths for a single node is not officially supported by Drupal, so I can understand if this issue will be rejected as a bug report. However there is really no reason multiple paths can't be supported and Drupal/pathauto don't actually prevent you from doing so by requiring the source entries in the url_alias table to be unique , and the fix for this issue is a very simple one-line change that avoids an unnecessary database lookup which it does currently, so I believe my patch is a general improvement and a bit of future proofing if not a bug.

Proposed resolution

Implement the following patch:

diff --git a/block.module b/block.module
index 4f498d3..eea61ef 100644
--- a/block.module
+++ b/block.module
@@ -779,7 +779,30 @@ function block_block_list_alter(&$blocks) {
       $pages = drupal_strtolower($block->pages);
       if ($block->visibility < BLOCK_VISIBILITY_PHP) {
         // Convert the Drupal path to lowercase
-        $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
+        $path = drupal_strtolower(substr($_SERVER['REQUEST_URI'], 1));
         // Compare the lowercase internal and lowercase path alias (if any).
         $page_match = drupal_match_path($path, $pages);
         if ($path != $_GET['q']) {

This replaces the reverse path lookup drupal_get_path_alias($_GET['q']) with the path taken from the actual server request $_SERVER['REQUEST_URI'], 1), avoiding a possible database hit and avoiding the possibility of getting incorrect results.

Remaining tasks

Assess the appropriate classification of this issue and any negative consequences of the fix.

✨ Feature request
Status

Postponed: needs info

Version

9.5

Component
BlockΒ  β†’

Last updated 3 days ago

Created by

πŸ‡³πŸ‡ΏNew Zealand scott.whittaker

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024