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

Sifro’s picture

Issue summary: View changes
Sifro’s picture

Issue summary: View changes
Dave Reid’s picture

Status: Active » Fixed

Setting 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.

/**
 * Alter the pattern to be used before an alias is generated by Pathauto.
 *
 * @param string $pattern
 *   The alias pattern for Pathauto to pass to token_replace() to generate the
 *   URL alias.
 * @param array $context
 *   An associative array of additional options, with the following elements:
 *   - 'module': The module or entity type being aliased.
 *   - 'op': A string with the operation being performed on the object being
 *     aliased. Can be either 'insert', 'update', 'return', or 'bulkupdate'.
 *   - 'source': A string of the source path for the alias (e.g. 'node/1').
 *   - 'data': An array of keyed objects to pass to token_replace().
 *   - 'type': The sub-type or bundle of the object being aliased.
 *   - 'language': A string of the language code for the alias (e.g. 'en').
 *     This can be altered by reference.
 */
function hook_pathauto_pattern_alter(&$pattern, array &$context) {
  // Switch out any [node:created:*] tokens with [node:updated:*] on update.
  if ($module == 'node' && ($context['op'] == 'update')) {
    $pattern = preg_replace('/\[node:created(\:[^]]*)?\]/', '[node:updated$1]', $pattern);
  }
}
Sifro’s picture

Awesome, 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

joshua.boltz’s picture

Came across this today, and I believe $module should be $context['module'], such as:

function hook_pathauto_pattern_alter(&$pattern, array &$context) {
  // Switch out any [node:created:*] tokens with [node:updated:*] on update.
  if ($context['module'] == 'node' && ($context['op'] == 'update')) {
    $pattern = preg_replace('/\[node:created(\:[^]]*)?\]/', '[node:updated$1]', $pattern);
  }
}