3

I have a rule that redirects users on login to a matching taxonomy term, ie. there is a taxonomy term that is identical to the user name and when a user logs in they are redirected to the url of that taxonomy term using tokens. The redirected url is as follows:

taxonomy/[account:name]

However, when there is a space in the user name then this causes problems, the URL of the taxonomy term has spaces automatically replaced with dashes, however the rule treats spaces as spaces and creates a url such as "mysite.com/taxonomy/first%20second"

Is there a way to replace the spaces with dashes?

4

1 Answer 1

1

Sometimes the solution to a problem, is to prevent the problem from happening.

So therefor I suggest you just do not allow spaces in your user names. To do so, use the Rules module (which you already have), and implement a rule that looks something like so:

{ "rules_disallow_spaces_in_user_names" : {
    "LABEL" : "Disallow spaces in user names",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "user_presave" : [] },
    "IF" : [
      { "text_matches" : {
          "text" : [ "account:name" ],
          "match" : "\\b \\b",
          "operation" : "regex"
        }
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "User names are not allowed to contains spaces, please correct",
          "type" : "error"
        }
      }
    ]
  }
}

Some more details about this rule (which you should be able to import in your own site using the Rules UI):

  • Rules Event: "Before saving a user account".
  • Rules Condition: use a regex to check of the user name contains a space (adapt the regex if needed).
  • Rules Action: set an appropriate error message (adapt if needed).

The additional advantage of this approach is also that you don't need the PHP filter for it to work (as suggested in one of the comments).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.