I needed more complex autoaliasing rules than the current template allows and hence decided to write support for using PHP scripts to generate them. The attached patch describes these changes against version 4.5.0.

It also adds a few new options:

  • Enabled: Activate/disable automatic aliasing of new nodes. It is a good idea to keep this unchecked while trying out different naming schemes.
  • Add only missing: Add aliases only if there are no aliases already? Note that you could do this check manually in PHP even if you answer 'no' here.
  • Test rules (below the "mass update checkbox"):Test the rules against all existing nodes. This only shows a list of generated aliases without actually adding any.

I don't know if this makes the module too complicated for your taste but I definitely needed this functionality so here it is.

As an example for the scripts, this is more or less what I currently use for effi.org's experimental Drupal site and it works like charm:

// Shorten the title if necessary and try to respect word boundaries
$title = preg_replace('/^EFFI:[ ]*/','',$node->title);
$title = strtolower(clean_alias($title));
$title = preg_replace('/^([a-z0-9-]{16,28})[^a-z0-9].*$/', '$1', $title);
$title = substr($title,0,28);

// The actual rules
if ( $node->type == "blog" )
  $res[] = "blog/" . clean_alias( "$yyyy-$mm-$dd-" .$node->name).".html";
else if ( $node->type == "story" )
  $res[] = "news/" . clean_alias( "$yy$mm$dd-" .$title) . ".html";
else
{
  if (isset($taxotree["Publication type"]["Press releases"])) 
    $res[] = "publications/releases/press-release". "-$yyyy-$mm-$dd.html";
  else if ( isset($taxopaths["Publication type"]))
    $res[] = clean_alias(strtolower( 
      $taxopaths["Publication type"][0]), '/') .'/' .$title . "-$yy$mm$dd.html";
  else if ( isset($taxotree["Activity theme"]) ||
      isset($taxotree["Publication type"]) )
  $res[] = strtolower($catpath . $title) . '.html';
}
CommentFileSizeAuthor
pathauto-php-script.diff17.12 KBelonen

Comments

elonen’s picture

Oh, this help text might assist in understanding the idea:

----

If the simple pattern language is too restricted for your needs, you can write here a PHP script that adds alias strings to an initially empty $res array variable. If this field is non-empty, the pattern above is ignored and the separator is only used for appending numbers when trying to avoid colliding names.

The script can use Drupal's fully populated $node object or the pattern placeholders, which are passed as ordinary PHP variables (e.g. [week] becomes $week). Additionally, it is provided with two slightly different views to full categorization, $taxotree and $taxopaths. Both are arrays with vocabulary names as first level keys. Under them, $taxopaths contains a string array of topic paths (e.g. strtolower($taxopaths['focus'][0]) might return 'sports/ski') whereas $taxotree has a multi level tree of term names plus key '1' for each selected term (example: isset($taxotree['focus']['sports']['ski'][1]) is True if term 'sport/ski' of vocabulary 'focus' is chosen). Function clean_alias($str, $sep='-') removes accents and replaces all non-ASCII characters with $sep, making $str suitable for use in a URL.

mikeryan’s picture

Assigned: Unassigned » mikeryan

I'll have to find some time to look over your suggestions in detail... The one thought off the top of my head when suggesting customization via PHP code is whether this would be better accomplished with a plugin approach, rather than exposing the less technical Drupal user to that level of complexity?

elonen’s picture

Perhaps, but it's actually quite convenient to develop the rules this way. Hmm.. One way to not scare non-programmers might be to hide the fields unless the user has checked some "Advanced"-checkbox?

mikeryan’s picture

I've completely refactored pathauto to use a more modular design. Please try out the CVS version of pathauto and let me know if it meets your needs without the need to inject PHP through the admin interface.

Thanks.

mikeryan’s picture