Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
We're using custom and contrib modules on our site (like role delegation) that render tabs with links like "users/[user-raw]/roles". What happens when a user changes their username is that a redirect is created if we have that option enabled, but this redirect only works for "users/[user-raw]". I'd like to suggest that if this redirect exists, that it should also work for any link that begins with "users/[user-raw]". Here's the code I worked on. Let me know your thoughts.
=== modified file 'drupal/sites/all/modules/path_redirect/path_redirect.module'
--- drupal/sites/all/modules/path_redirect/path_redirect.module 2010-12-23 14:43:04 +0000
+++ drupal/sites/all/modules/path_redirect/path_redirect.module 2012-03-29 16:30:29 +0000
@@ -385,6 +385,20 @@
* An optional query string to match.
*/
function path_redirect_load_by_source($source, $language = '', $query = array()) {
+ // Route user redirects properly for user links like users/[user-raw]/roles.
+ // Right now, this will only work with the default pathauto user pattern.
+ $arg_check = arg(2);
+ if (substr($source, 0, 5) == 'users' && module_exists('pathauto') && variable_get('path_redirect_user_wildcard', TRUE)
+ && variable_get('pathauto_user_pattern', 'users/[user-raw]') == 'users/[user-raw]' && !empty($arg_check)) {
+
+ preg_match('/(users\/.*?)(\/.*)/', $source, $matches);
+ $result = db_result(db_query("SELECT redirect FROM {path_redirect} WHERE source = '%s'", $matches[1]));
+ if ($result) {
+ // We have a redirect, so redirect user immediately.
+ drupal_goto(drupal_get_path_alias($result) . $matches[2], NULL, NULL, 301);
+ }
+ }
+
$where = $query ? "(source = '%s' OR source LIKE '%s%%')" : "source = '%s'";
$args = $query ? array($source, $source . '?') : array($source);
$args[] = $language;
Comment | File | Size | Author |
---|---|---|---|
#1 | wildcard-redirect-user-links-1508516-0.patch | 1.24 KB | djbobbydrake |
Comments
Comment #1
djbobbydrake CreditAttribution: djbobbydrake commentedAttached is the git patch.
Comment #2
djbobbydrake CreditAttribution: djbobbydrake commented