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
  • this is probably why most sites do not allow spaces in the user name Commented Jul 3, 2016 at 21:43
  • You will have to use Rules with PHP to accomplish this. Commented Jul 3, 2016 at 21:44
  • So can I just write PHP code into this field or is there a separate module? Commented Jul 3, 2016 at 22:05
  • you need to enable PHP Filter module, which I am not a fan of. What are the downsides of using 'custom' PHP code in blocks, nodes, views-args, etc? then you will be able to write php evaluation in Rules Commented Jul 3, 2016 at 22:21

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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.