Problem/Motivation
I noticed that during updating a node, pathauto checks for usable patterns before the node is updated in the database. That means if it ever compares entity field values, it doesn't use the updated ones.
Steps to reproduce
How I discovered that:
PathautoWidget calls $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
and the usable patterns are saved to $patterns property in PathautoGenerator. But the $entity parameter here doesn't contain the new field values, yet.
Later, in pathauto_entity_update hook, the \Drupal::service('pathauto.generator')
is called again and there is no check for possible patterns, the "cached" $patterns property value is used. Shouldn't the module check for patterns only now that the new field values are available?
There's one case I encountered where this causes problems: with manually programmed custom conditions added to a pattern. Conditions like checking if a field has a specific value.
For example:
- Pattern1 is used for article node & when field_category = Books
- Pattern2 is used for article node & when field_category = E-Books
What happens in the example is:
- when updating article node1 I change the field_category to E-Books from Books
- after I click save, the alias is not updated, it still uses Pattern1 (because when the getPatternByEntity function checked patterns, it didn't use the newly saved node, and field_category was still Books)
- if I open node1 and click save again without changing anything, the alias is updated using Pattern2 (because when the getPatternByEntity function checked patterns, field_category was E-Books before and after saving the node)
Proposed resolution
Reset the cached pattern for the entity before updating its alias.
Remaining tasks
Cover this case with tests?
User interface changes
No.
API changes
No.
Data model changes
No.