Closed (fixed)
Project:
Redirect
Version:
8.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
15 Sep 2017 at 15:56 UTC
Updated:
1 Jun 2025 at 11:25 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
dpagini commentedI didnt want to keep the
hook_redirect_alter()hook name, b/c that conflicts withhook_ENTITY_TYPE_alter()(even though that hook doesn't exist, it just seems too close to the entity API hook names), so I decided to usehook_redirect_response_alter()as my hook name.I tried to cleanup the API docs just a little bit, but this is where I'm the least confident. I also added a test for my hook. I used some other modules like hooks_example, coffee and domain to figure out where/how to build tests. I also created a RedirectTestBase so I could re-use the assertRedirect() method.
Comment #3
dpagini commentedComment #4
dpagini commentedI also cleaned up the RedirectRequestSubscriber. There were a bunch of DI services not being used, unused "use" statements, and an unused protected method. I can revert any of these changes if we want to keep that stuff in there, but it all really just felt like clutter to me.
Comment #6
dpagini commentedOops, patch was created with dirty branch. Think this fixes it.
Comment #7
dpagini commentedSorry, I'm still not great at creating my own patches. Missed the new files in the last patch.
Comment #9
dpagini commented(sorry for all the comments)
Updates other broken tests with the DI updates.
Comment #10
dpagini commentedWoohoo, I've got a pass... Ha. Sorry again for all the comments. Please let me know what you think.
Comment #11
berdirCan you maybe explain a bit more what your use case for this is? What do you think that hook should allow to do?
I could imagine that a standard response event subscriber might also work, but you for example wouldn't have access to the redirect entity and it might not be trivial to identify whether a redirect is actually coming from redirect.module, possibly by checking some headers.
A possible option would be to either have a custom redirect class that also contains the redirect entity and/or have a custom event, we probably shouldn't add "new" hooks.
Comment #12
dpagini commentedSure.... so, I realized I don't have an urgent need for this about 75% through building this, but am still going to utilize the patch as is now (or if it gets reworked).
Here is a similar, but probably better use case...
I have a large site using the domain module, with many domains. I want to limit redirects to only be valid on certain domains. I can extend the Redirect entity with a new domain reference field. I can use ALL the utility of Redirect as it's built, but I want to make sure that redirects are somehow limited by domain (either ensure the redirect incorporates the domain name, or is limited to the domains selected, etc.). When this hook is fired, I can check the redirect entity, along with the response that's about to be sent to the user. In this case, maybe I want to even cancel the redirect altogether and just serve a page not found.
I hope that helps?
Comment #13
amaisano commentedI'd like to be able to strip or alter certain query parameters during the redirect execution, so that the destination does not have for instance "id=5" in it (I want this for certain redirects - otherwise use the global "preserve params" option).
Comment #14
awolfey commentedI can use this, at least temporarily, to avoid what's happening in #2984272: Dots in query parameter names converted to underscores.
Comment #15
dpagini commentedRe-rolled for latest version. Would love to get some feedback on the idea of this patch. Happy to put more work in here as well.
Comment #17
dpagini commentedComment #18
kenorb commented(removed, ignore this)
Comment #19
dpagini commentedDid you try a cache clear? I've seen that type of error before and it's usually fixed with a `$ drush cr`. If this were truly broken, I would expect the test to fail as well.
Comment #20
kenorb commentedMaybe it's my Docker environment, will re-test that tomorrow.
Comment #21
kenorb commentedI think it was some file sync error with Docker. Secondly I was testing the wrong hook (D7 version of it instead of new one).
I've tested hook_redirect_response_alter() and I've got the dump of $redirect, which is correct.
Comment #22
kenorb commentedI've managed successfully implement
hook_redirect_response_alterusing the #17 patch in order to implement token support and it works.More information at: #1331582-12: Use tokens for redirects
Comment #23
berdirSorry, but this is referencing deprecated code, should use \Drupal::messenger().
This removes a lot of unrelated code, I'd rather keep this issue to a minimum and clean it up separately, easier if someone ever needs to debug that and I need to check whether this code really can be removed.
I'm also wondering if the canonical redirect handling would also need an alter hook. And still not 100% convinced the hook is the right way to go. Just thinking out loud, an option might be to have a custom redirect class that has the redirect entity included, so that people can implement response events instead and check for that type of response.
Comment #24
jcmartinezThis patch provides a very quick way of implementing redirect logic. It is very useful on my project that uses the domain access module and I'd love to see this patch make it to a release.
One thing that I would like to understand better is the following mention by dpagini:
I have a case where I want for the redirect to fire only under a specific set of conditions; otherwise, I want to serve the page without redirecting. How can I cancel the redirect and serve the original page?
Comment #25
Quicksaver commentedRe-rolled patch #17 to apply to the latest 1.4.0 release.
Comment #26
fabiansierra5191 commentedI tested the patch #25 and the redirect.api.php file was updated and the hook_redirect_response_alter is working when you create it.
Comment #27
fabiansierra5191 commentedComment #28
Phil Wolstenholme commented#25 no longer applies against 1.5.0
Comment #29
angrytoast commentedUpdating with a re-roll of #25 to latest changes in 8.x-1.x. This should fix the patch issue per #28
This addresses the concerns in #23. It removes the changes that refactor out unnecessary class properties and dependency injection. This is because it involves both dependency deprecation which is getting addressed in https://www.drupal.org/project/redirect/issues/3102143, but also unused dependencies, which while it is cruft, doesn't need to be addressed in this feature addition.
Comment #30
berdirthis needs to use the new compatibility key like other modules to be D9 compatible.
Sorry for the nitpick and slow response.
Comment #31
angrytoast commentedNot at all. Here is a patch to update
redirect_test.info.yml'score_version_requirementto match the mainredirect.info.ymlComment #32
bmango commentedPatch applied cleanly for me (on version 1.4). Many thanks!
Comment #33
recrit commentedAdded a "$defaultTheme" to "tests/src/Functional/RedirectHooksTest.php" in order to be D9 compatible.
Comment #36
paulocsI created a MR for this issue.
I changed the parameter in the documentation file
redirect.api.php... I also changed it in the tests.Replaced
RedirectResponsewithTrustedRedirectResponse.Comment #37
gabrieldaReviewing this one
Comment #38
gabrieldaI've successfully implemented MR!15. This correctly adress the issue. Moving to RTBC.
Comment #41
berdirMerged, thanks.
Comment #43
jweowu commentedAs part of my re-roll of https://www.drupal.org/project/redirect/issues/2957751#comment-15248419 I wrote the following commit:
https://git.drupalcode.org/issue/redirect-2957751/-/commit/2de86d1ef5ce2...
At the time that had seemed to me like a bug fix for this issue, so I'm leaving a comment about it here so that the appropriate people can review/confirm whether this change should be applied, as it looks like it was left out of the subsequent re-roll that was ultimately committed for that other issue.