Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
We added a proper URL alias API with hook_path_insert/update/delete, but we do not have a way to alter an alias before its saved. This could make things tremendously easier for Pathauto in D7 since modules like Transliteration could implement hook_path_alter() to change non-standard characters in an alias.
Comment | File | Size | Author |
---|---|---|---|
#24 | missing_necessary_way-683510-24.patch | 1.83 KB | nironan |
#18 | 683510-hook-path-alter-D8-18.patch | 1.4 KB | ao2 |
Comments
Comment #1
Dave ReidPatch for review that adds hook_path_alter() invoked from inside path_save(). Also fixes a bug with locale.module causing aliases to be saved with an empty string for language instead of LANGUAGE_NONE ('und').
Comment #2
gregglesThis seems like a a great addition. A lot of what makes Pathauto painful is that neither it nor the path module have sufficient hooks. This is a great improvement.
Comment #3
Dave Reid#1: 683510-hook-path-alter-D7.patch queued for re-testing.
Comment #4
Dave ReidRe-rolled for the fixed locale section.
Comment #5
Dave ReidTrivially re-rolled for HEAD only.
Comment #6
webchickI think a hook here makes sense, but...
1. Seems like it would fit the pattern of _save() more than _alter(). _alter() tends to be when the thing is built and about to be presented, where this is more along the lines of hook_node_save()/hook_user_save().
2. We need fleshed out example docs here.
Comment #7
Dave ReidThat's true for things that are presented, but URL aliases are not something that is 'visible'. We use '_alter' for non-visible things like menu items, etc. I can add more docs, although the same standard was used to add the URL alias documentation in the first place (see http://api.drupal.org/api/function/hook_path_insert/7).
Comment #8
Damien Tournoud CreditAttribution: Damien Tournoud commentedI agree with Angie here. This hook should be hook_path_presave().
Comment #9
Dave ReidHrm, it just doesn't make sense to me when we have hook_menu_link_alter (before it's saved), hook_menu_alter (before it's saved), hook_entity_info_alter (before its cached). Plus we cannot use module_invoke_all() with an array that needs to be altered by reference + PHP 5.2. It has to be using drupal_alter(). Thats why those other hooks use _alter.
Comment #10
Dave ReidThis is too late for D7 anyway at this point, although since this isn't going to break any backwards compatibility, would be nice to have considered for a major post-7.0 Drupal release down the road.
Comment #11
Dave ReidAdding tags...
Comment #12
Dave ReidWell, I guess this is too late now. I have worked on the bare minimum we need to unblock the D7CX versions of Pathauto and Redirect modules that doesn't change any APIs: #998256: Please let modules know about the original URL alias in hook_path_update().
Comment #12.0
CrookedNumber CreditAttribution: CrookedNumber commentedFixing typo.
Comment #13
chx CreditAttribution: chx commentedbump.
Comment #16
nikunjkotechaAdded D7 patch for latest 7.x code.
Comment #18
ao2 CreditAttribution: ao2 as a volunteer commentedHi,
I am attaching a patch for drupal 8.
Are we still in time to have this in drupal 8.3?
A hook like this would be very useful for modules like Neutral paths, for instance.
Thanks,
Antonio
Comment #19
ao2 CreditAttribution: ao2 as a volunteer commentedThinking about it, it could even make sense to allow modules to play with
$fields['pid']
inhook_path_alter()
, I can think of two use cases:$fields['alias']
of a newly inserted path already exists in the database, and if so sets$fields['pid']
to avoid a new insertion with the same alias triggering an update instead;$fields['pid']
is not NULL, detecting an update, and changes the language following some criterion (e.g. retrieve the original path to preserve the old language, in the spirit of #541802: Let path_nodeapi reuse the current path alias language on update);These can be supported with something like this:
This change can be either merged with the patch in #18 or committed as a preparatory commit before it.
Comment #20
Xen CreditAttribution: Xen at Reload commentedDoesn't seem to me that adding a hook is quite the Drupal 8 way.
Overriding the AliasStorage service with your own implementation is one way to get more control, but it'll get messy if pathauto adopts that approach.
Seems to me that path module is lacking an API. It should delegate path mangling to interested parties. It's also a bit messy as it is, while the path is implemented as a field, it's the widget that finds the default value.
Comment #24
nironan CreditAttribution: nironan commentedNew D7 patch based on nikunjkotecha's (#16): the altered path can now be erased.
Comment #30
smustgrave CreditAttribution: smustgrave at Mobomo commentedAgree with the last comment not sure a hook is what is needed here.
Think an IS update is needed.