Closed (fixed)
Project:
Pathauto
Version:
4.5.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Feature request
Assigned:
Reporter:
Created:
10 Mar 2005 at 14:28 UTC
Updated:
5 Jun 2005 at 15:53 UTC
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:
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';
}
| Comment | File | Size | Author |
|---|---|---|---|
| pathauto-php-script.diff | 17.12 KB | elonen |
Comments
Comment #1
elonen commentedOh, 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,$taxotreeand$taxopaths. Both are arrays with vocabulary names as first level keys. Under them,$taxopathscontains a string array of topic paths (e.g.strtolower($taxopaths['focus'][0]) might return'sports/ski') whereas$taxotreehas 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). Functionclean_alias($str, $sep='-')removes accents and replaces all non-ASCII characters with$sep, making$strsuitable for use in a URL.Comment #2
mikeryanI'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?
Comment #3
elonen commentedPerhaps, 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?
Comment #4
mikeryanI'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.
Comment #5
mikeryan