Hi,
I've created this custom function:
function HOOK_pathauto_alias_alter(&$alias, array &$context) {
if( isset($context['data']['node']->field_MyField['und']) ) {
variable_set('pathauto_node_MyType_pattern', 'something/[node:title]');
}
else {
variable_set('pathauto_node_MyType_pattern', 'something-else/[node:title]');
}
}
When I try to bulk update the aliases, sometimes it picks the right patterns, sometimes it doesn't.
My guess is that if a node uses the 1st pattern, then the following node will use the 1st one too, even if it matches the 2nd condition of the IF.
Then the following node will use the 2nd pattern (because the previous one matched the 2nd condition even if it used the 1st pattern), and so on...
Basically, the "variable_set" part only works for the node that will be processed AFTER.
So my question is: is there a way to alter the pattern for a single alias before the alias is saved?
I've also tried setting
$context['pattern'] = 'mypattern/[node:title]'
but without success.
Comments
Comment #1
Sifro CreditAttribution: Sifro commentedComment #2
Sifro CreditAttribution: Sifro commentedComment #3
Dave ReidSetting variables in a hook like this is a bad idea, and not to mention, happens too late for it to actually affect the alias being generated. There is an actual hook for you to use instead, you should use that one instead of the alias alter hook.
Comment #4
Sifro CreditAttribution: Sifro commentedAwesome, this work perfectly, thanks a lot!
I should have seen this HOOK in the api.php file, but I didn't think about checking against the -dev version.
There were several sources on the net suggesting the method in #1, I'll point them here.
Comment #6
joshua.boltz CreditAttribution: joshua.boltz commentedCame across this today, and I believe $module should be $context['module'], such as: