Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm creating a Drupal 7 site which will consist of information about many different organizations across North America. The profiles will be submitted by the organizations themselves and it is very likely that there will be duplicate names.

What I'd like to do is have Pathauto create the url: orgs/OrganizationName for the first such organization and after this create: orgs/OrganizationName-City.

Is there any way to change how Pathauto behaves when duplicates are found?

share|improve this question
    
there is an option to define the pattern of the alias have you tried that? –  Mohammed Shameem Jan 17 '13 at 16:17
    
Yes of course, what I'm asking is is there a way to define a second alias when the first results in a url that already exists. –  Godwin Jan 17 '13 at 17:17
add comment

1 Answer 1

up vote 1 down vote accepted

You could create a module and use the hook_pathauto_alias_alter function to override the path alias before saving it.

function mymodule_pathauto_alias_alter(&$alias, &$context) {

  $temp = $alias;
  $newalias = pathauto_alias_uniquify($temp, $context['source'], $context['language']);
  if ($newalias !=$alias)
  {
    $alias .= $context['data']['node']->field_city[$context['data']['node']->language][0]['value'];
  }

}

The pathauto_alias_uniquify function checks if there is an alias already existing and if it does then would return the updated alias and if not return the same alias. now the one you get it different from the one we already have then we would could append the city name to the alias.

I have created a field called city with a field machine name field_city you will have to change your values accordingly.

share|improve this answer
    
Thanks @Mohammed, I was hoping that there was a module already that would do this for me but failing that I guess I'll just make my own (and submit it to Drupal if I can work out how to alter the existing forms). –  Godwin Jan 17 '13 at 21:32
    
the one problem with your code is that pathauto_alias_uniquify doesn't return the updated alias name but it is put in $temp by reference. –  Godwin Jan 18 '13 at 16:34
    
have you checked it? because when I tested it, it returns the new alias. –  Mohammed Shameem Jan 18 '13 at 16:38
    
I have, however perhaps there we're running different versions and there was some change. I'm on 7.x-1.2. Not a huge change anyway, it just threw me off (if ($temp != $alias)...) –  Godwin Jan 18 '13 at 17:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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