I would like an alter hook so that I can limit the scope of the behavior of path_alias_xt in another module. For example, to run additional regular expression validation on the urls. The reason this is helpful is that the default behavior of the path_alias_xt allows any additional url arguments on any url. It would be good to be able to limit the scope of this with custom code.

I propose to add one line to the module:

diff --git a/modules/path_alias_xt/path_alias_xt.module b/modules/path_alias_xt/path_alias_xt.module
index d2aee66..6b58b98 100755
--- a/modules/path_alias_xt/path_alias_xt.module
+++ b/modules/path_alias_xt/path_alias_xt.module
@@ -106,6 +106,7 @@ function path_alias_xt_url_inbound_alter(&$path, $original_path, $path_language)
           $src .= '/' . $user->uid;
         }
         $path = $src . drupal_substr($original_path, $pos);
+        drupal_alter('path_alias_xt_url_inbound_path', $path, $original_path, $path_language);
         return;
       }
     }

Then I can add additional validation, for example, limiting the scope of the path_alias_xt to certain url patterns with regular expressions.

CommentFileSizeAuthor
#1 path_alias_xt-allow_alter-2207815-1.patch568 bytesoknate

Comments

oknate’s picture

StatusFileSize
new568 bytes
oknate’s picture

Here's a use case for this patch. I want to limit the path_alias_xt from working on all other paths, except if they have "slide/1" on the end, for example. That way, if I have a valid url such as "videos", an extended alias that I don't want, such as "videos/my-random-string" will 404, as it should. Right now, out of the box, path_alias_xt doesn't have a way to limit this. This is why I'm proposing an alter hook.

This is how I'm planning to use it, in another module called path_alias_xt_limiter:

function path_alias_xt_limiter_path_alias_xt_url_inbound_path_alter(&$path, $original_path, $path_language) {
  
  if($path != $original_path) {
      
    if (preg_match('/\/slide\/(.+)$/',$path, $matches)) {
      $slide_number = $matches[1];
      
      if (!is_numeric($slide_number) || $slide_number < 2) {
        $base_path = preg_replace('/\/slide\/(.+)$/', '', $path);
        drupal_goto($base_path, array(), '301');
      }
      
      if ($slide_number && path_alias_xt_limiter_is_valid_slide($slide_number, $path)) {
        return $path;
      }
    } else { // if the path doesn't have /slide/<some number> in it, undo path_alias_xt changes
      $path = $original_path;
    }
      
  }
  
}

function path_alias_xt_limiter_is_valid_slide($slide_number, $drupal_path) {

  if (preg_match('/node\/([0-9]+)/',$drupal_path,$matches)) {
    $nid = $matches[1];
    $node = node_load($nid);
    
    if ($node->type == 'gallery' && isset($node->field_gallery_images[LANGUAGE_NONE][$slide_number]['value'])) {
      return TRUE;
    }

  }
  return FALSE;
}
rdeboer’s picture

Hi oknate,

Thank you for your suggestion.

But you can already do this by implementing the same function that path_alias_xt_url_inbound_alter(&$path, $original_path, $path_language) implements, namely hook_url_inbound_alter().

All you have to do is to make sure that your hook is called after path_alias_xt's hook.
You do this by making sure the module weight of your module is higher than the weight of path_alias_xt, which it will be by default as the weight of path_alias_xt is -1 and the default weight for modules is zero.

Or am I missing something?

Rik

oknate’s picture

Rik,

Yes, you're right. As long as the module weight is lower. So if path_alias_xt is -1, my module that makes changes needs a lower weight to fire after it, say -2. In this case, the patch isn't needed. It needs to be lower because drupal_get_normal_path runs the modules in reverse order.

from drupal_get_normal_path:

  // Allow other modules to alter the inbound URL. We cannot use drupal_alter()
  // here because we need to run hook_url_inbound_alter() in the reverse order
  // of hook_url_outbound_alter().
  foreach (array_reverse(module_implements('url_inbound_alter')) as $module) {
    $function = $module . '_url_inbound_alter';
    $function($path, $original_path, $path_language);
  }

I had the purl module enabled which was changing the $original_path variable, so I thought I couldn't do it with a simple hook_url_inbound_alter, but I think that's just an issue with the purl module, that was causing the $original_path variable to change before getting to my custom module.

rdeboer’s picture

Status: Active » Closed (works as designed)

Hi oknate,
Thank your for your response. In that case I'm closing this issue.
Rik