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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Status: Active » Needs review
FileSize
2.31 KB

Patch 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').

greggles’s picture

Status: Needs review » Reviewed & tested by the community

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

Dave Reid’s picture

#1: 683510-hook-path-alter-D7.patch queued for re-testing.

Dave Reid’s picture

Re-rolled for the fixed locale section.

Dave Reid’s picture

Trivially re-rolled for HEAD only.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

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

Dave Reid’s picture

That'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).

Damien Tournoud’s picture

I agree with Angie here. This hook should be hook_path_presave().

Dave Reid’s picture

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

Dave Reid’s picture

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

Dave Reid’s picture

Issue tags: +pathauto, +API addition

Adding tags...

Dave Reid’s picture

Version: 7.x-dev » 8.x-dev

Well, 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().

CrookedNumber’s picture

Issue summary: View changes

Fixing typo.

chx’s picture

Issue summary: View changes

bump.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nikunjkotecha’s picture

Added D7 patch for latest 7.x code.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ao2’s picture

Title: Missing necessary way to alter URL aliases before saving » Add hook_path_alter()
Status: Needs work » Needs review
FileSize
1.4 KB

Hi,

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

ao2’s picture

Thinking about it, it could even make sense to allow modules to play with $fields['pid'] in hook_path_alter(), I can think of two use cases:

  • a hook implementation checks if the $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;
  • a hook implementation checks if the $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:

diff -u b/core/lib/Drupal/Core/Path/AliasStorage.php b/core/lib/Drupal/Core/Path/AliasStorage.php
--- b/core/lib/Drupal/Core/Path/AliasStorage.php
+++ b/core/lib/Drupal/Core/Path/AliasStorage.php
@@ -67,13 +67,14 @@
       'source' => $source,
       'alias' => $alias,
       'langcode' => $langcode,
+      'pid' => $pid,
     ];
 
     // Allow other modules to alter the path.
     $this->moduleHandler->alter('path', $fields);
 
     // Insert or update the alias.
-    if (empty($pid)) {
+    if (empty($fields['pid'])) {
       $try_again = FALSE;
       try {
         $query = $this->connection->insert(static::TABLE)
@@ -102,17 +103,16 @@
       // Fetch the current values so that an update hook can identify what
       // exactly changed.
       try {
-        $original = $this->connection->query('SELECT source, alias, langcode FROM {url_alias} WHERE pid = :pid', [':pid' => $pid])
+        $original = $this->connection->query('SELECT source, alias, langcode FROM {url_alias} WHERE pid = :pid', [':pid' => $fields['pid']])
           ->fetchAssoc();
       }
       catch (\Exception $e) {
         $this->catchException($e);
         $original = FALSE;
       }
-      $fields['pid'] = $pid;
       $query = $this->connection->update(static::TABLE)
         ->fields($fields)
-        ->condition('pid', $pid);
+        ->condition('pid', $fields['pid']);
       $pid = $query->execute();
       $fields['original'] = $original;
       $operation = 'update';

This change can be either merged with the patch in #18 or committed as a preparatory commit before it.

Xen’s picture

Doesn'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.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

nironan’s picture

New D7 patch based on nikunjkotecha's (#16): the altered path can now be erased.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update

Agree with the last comment not sure a hook is what is needed here.

Think an IS update is needed.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.