The Pathauto generates an alias according to the title of the item. Rather, I would like it to generate a random number (or number + letters), so the user gets url as:

- mydomain.com/k57js
- mydomain.com/9wn71
etc.

I was searching for such a module or code snippet, but could not find one. Any ideas where to look for or how to do it myself?

Thanks!

Comments

coreyp_1’s picture

Use CCK Computed Field to generate a random text string. It will require a few lines of PHP coding.

Pathauto will offer a token that you can use that will put the contents of that Computed Field into the URL. Don't worry about checking for duplicates... Pathauto will take care of that for you, and add an extra "_0", "_1", "_2", etc. if needed.

-Corey

amir simantov’s picture

Thanks Corey. Where should I then put the php code and what should it be? Any reference or snippet?
Thanks again!

BTW - Are there any other ways to accomplish the task except using CCK?

amir simantov’s picture

And here is the function I have used:

function genRandomString() {
    $length = 5;
    $characters = ‘0123456789abcdefghijklmnopqrstuvwxyz’;
    $string = "";    

    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }

    return $string;
}

$node_field[0]['value'] = genRandomString();
frames’s picture

Just out of curiosity. Why do you need/want to do this? You could just use the node id, which is available in pathauto straigh away if you do not want a long string of characters.

It looks to me that you are complicating your life and your users' to some extent.

And by the way, random does not mean unique, although I think pathauto will avoid a second path with the same "random" combination, I'm not 100% sure it'll do; it might depend on how you implement this.

amir simantov’s picture

I do not want people which don't have a specific link to browse for content by node id. This will not prevents bots, but it is private enough.

frames’s picture

I knew there was a reason.
:-)

Is it OK for you using a running number by node type instead of using node id as I suggested? If so, there's a module for that already:

http://drupal.org/project/type_local_nids

Hope that helps.

frames’s picture

http://drupal.org/node/105049

Although that's for D5.

amir simantov’s picture

It actually helps users track the nodes in my situation...