Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
If you implement hook_pattern_alter()
, and change the pattern, then because the pattern is static cached, your altered pattern will take affect even when you don't want it to. This won't be a problem if a single path is being generated, but if multiple paths are being generated in a single HTTP request (e.g. bulk (re)generation, or migrations), then the (altered) statically cached pattern will always take affect, even when the conditions within your alter hook are not met.
Example implementation where this bug happens:
/**
* Implements hook_pathauto_pattern_alter().
*/
function my_module_pathauto_pattern_alter(\Drupal\pathauto\PathautoPatternInterface &$pattern, array $context) {
/* @var $node Drupal\node\Entity\Node */
if ($context['module'] !== 'node' || !($node = $context['data']['node']) || $node->bundle() !== 'resource') {
return;
}
if ($node->field_issues) {
// Our new pattern is almost the same, but without the leading "education/".
$pattern->setPattern(preg_replace('!^education/!u', '', $pattern->getPattern()));
}
}
Comment | File | Size | Author |
---|---|---|---|
#2 | 2950643-pathauto-bulk_generation_errors-2.patch | 1.17 KB | dalin |
|
Comments
Comment #2
dalinComment #3
BerdirYeah, that's exactly why the hook like this is problematic. But it's there, the module is stable and we documented it now like this, so we have to live with it.
Thought about a test, but not trivial to set up I suppose and at least we it doesn't break anything we have tests for.
Comment #5
Berdir