Problem/Motivation

Modules can implement hook_pathauto_pattern_alter to alter a pathauto pattern. How ever, it is not possible to alter or add contexts that can be used in the token replacement.

Proposed resolution

Use the reference $context version of the token data $context['data'].

CommentFileSizeAuthor
pathauto_allow_context_alter.patch703 bytesharlor

Issue fork pathauto-3217226

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Harlor created an issue. See original summary.

james.williams’s picture

Status: Active » Reviewed & tested by the community

Works great from my point of view - thank you!

berdir’s picture

Status: Reviewed & tested by the community » Needs work

makes sense but all contributions must be done as merge requests now to ensure that current tests still pass.

james.williams’s picture

Status: Needs work » Reviewed & tested by the community

MR !84 is ready then :)

matthiasm11’s picture

+1 for this patch / merge request.

mably made their first commit to this issue’s fork.

mably’s picture

Title: Allow contexts to be alterd in hook_pathauto_pattern_alter » Allow contexts to be altered in hook_pathauto_pattern_alter
mably’s picture

Change record: hook_pathauto_pattern_alter() context data

What changed

In PathautoGenerator::createEntityAlias(), the token replacement call now uses $context['data'] instead of the original $data variable. This means modules
implementing hook_pathauto_pattern_alter() can now modify $context['data'] to alter the token data used for alias generation.

Before

The $context array (including 'data') was passed to the alter hook, but token->replace() used the original $data variable. Any modification to $context['data'] in the hook was silently ignored.

After

token->replace() now reads from $context['data'], so alter hook modifications take effect. For example, a module can inject additional token data to be used with an altered pattern:

function mymodule_pathauto_pattern_alter(&$pattern, $context) {
    // Add custom token data that will now be used during alias generation.
    $context['data']['custom_entity'] = $some_entity;
  }

Backward compatibility

  • No impact if no module implements hook_pathauto_pattern_alter(), or if the hook doesn't modify $context['data'] — behavior is identical.
  • Potential impact if a module was modifying $context['data'] for its own purposes (e.g. logging) without intending it to affect token replacement — those changes will now produce different aliases.

mably’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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