Postponed
Project:
Redirect
Version:
8.x-1.x-dev
Component:
Documentation
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
4 Nov 2011 at 08:58 UTC
Updated:
28 Mar 2022 at 14:57 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
davidwhthomas commentedI added support for user tokens using the drupal_alter call in the redirect module.
In another module I added
I added a redirect like
from: my-subscription
to: user/[user:uid]/subscription
and it works fine :)
I can even make a link like
and the user is bounced there to their user profile subpage
If the user is already logged in, they go staight to the dest page.
If the user is logged out, they get bounced via user/login and onto the dest page
Cheers,
DT
Comment #2
henrijs.seso commentedThanks for the code. Still, would be welcome addition.
Comment #3
zastapmnie commentedHello
How excatly You "added support for user tokens using the drupal_alter call in the redirect module".
I asking because i'm completly green in programming :_)
Best Regards
Comment #4
thejamesjones commentedDave,
Where exactly did you add this? in the redirect.module file? between what lines?
James
Comment #5
davidwhthomas commentedI added the code in a custom module, replace "EXAMPLE_" with the module name.
More info:
DT
Comment #6
spessex commentedHi David
I came across your post with much interest and have used your code and have created the module to use tokens on a redirect.
I am currently trying to use it to redirect from the edit page of a Profile2 profile e.g. 'profile-live_surveys/[profile2:user:uid]/edit' but it doesn't appear to work for me.
I'm trying to get to the bottom of this but for a starter wanted to know if the 'URL Redirects' page still 'counts' the redirects if you are using a redirect with a token or does it not get counted?
I've tried to test it with some other token but for the life of me can't think of one to use/test to see if it's working. My code is as follows as simply replaces EXAMPLE as you stated - my module is called ste_survey_edit_redirect (please note I have excluded the closing php statement as advised by Drupal for modules):
<?php
/**
* Implements hook_redirect_alter
*/
function ste_survey_edit_redirect_redirect_alter(&$redirect){
global $user;
// Only act if user token in redirect path
if(!empty($redirect->redirect) && strpos($redirect->redirect, '[user') !== FALSE){
if(!user_is_logged_in()){
// Bounce via login back to source path
$redirect->redirect = 'user/login';
$redirect->redirect_options['query'] = array('destination' => $redirect->source);
}else{
// Apply user tokens to redirect path
$redirect->redirect = token_replace($redirect->redirect, array('user' => $user));
}
}
}
Comment #7
pere orgaI've closed #1446236: Is there a way to use a token in the redirect? as a duplicate of this one
Comment #8
pere orgaThis would be a great addition. However, It would be so complex to implement that it may be a good idea to create a Redirect Token contrib module instead.
That's why I think this ticket should be postponed, at least until a stable version is released. Imho
Comment #9
ndf commentedJust made a module based on the snippets above:
https://www.drupal.org/project/redirect_token
Comment #10
dave reidComment #11
socialnicheguru commentedcan this be updated to Drupal 8?
Comment #12
kenorb commentedExample code for Drupal 8:
To use hook_redirect_response_alter, you need to apply patch from #2909182: API migration: No hook_redirect_alter() in 8.x first.
Testing scenario:
1. Create a redirect with the following parameters:
FROM: token/test
TO: /success/[current-page:url:relative]
2. Now, when you go to /token/test, you should be redirected to /success/token/test.
Here is the use case example: How to dynamically redirect user from /foo/X to /bar/X?
Comment #13
chris matthews commentedComment #14
alternativo commentedHi,
I tried patch #25 both with version 1.4 and dev, but no way to make tokens work in all your suggestions and also test.
Version 1.4: It seems not to work token functionality at all, it do not covert any token, the url remain of this kind '%5Bcurrent-page%3Aurl%3Arelative%5D'
Dev version: It recognises the [] so the url takes: [current-page%3Aurl%3Arelative]
Do anyone can help me...think I did something wrong beside the patch and hook_redirect_response_alter funcion
thanks
Comment #16
angrytoast commentedComment #17
angrytoast commentedFor @alternativo in #14 and any one who might stumble on this thread. There is a new patch (as of 2020-01-30) in https://www.drupal.org/project/redirect/issues/2909182#comment-13444722 (#29) that should be compatible with the latest 8.x-1.x redirect version.
I have verified it is working, you have to:
1. Apply the patch to the module, this enables a net new alter hook,
hook_redirect_response_alter2. You must implement a hook function in a custom module that you control. @kenorb's comment in https://www.drupal.org/project/redirect/issues/1331582#comment-12873791 is a good starting point. The redirect module does not care about tokens by default, so it is the site owner's responsibility to implement custom behavior.
Comment #18
alternativo commentedHi @angrytoast, thanks I will test the new patch asap, and I will send test results.
Meanwhile I solved using redirect in .htaccess file, but I think it will be better to use redirect module, as the .htaccess file is replaced on modules/core updates :/
Comment #19
andrew answer commentedHi, maybe someone will be interested in my solution. I use Pathauto module installed, and use this code:
to generate path with pattern /owner/[user:field_user_city:value]/[user:uid]-[user:field_user_firstname:value]. Then I create redirect with
Comment #20
redzeufHere is a proposition to go forward on doing a patch feature that take care of tokens in the "sub_path" and "url destination". You can find those fields at this path /admin/config/search/redirect/domain and the Tokens list available here: /admin/help/token
Please be aware that I only take care of "global" token like for exemple "site:", "language:"... So I do not pass any array $data and by the way I don't care of all token cases. Lot of tokens won't be replaced.
Read the documentation about the token replace function for more information.
Comment #21
redzeufJust a review of the header of the patch because past one doesn't apply... + I renamed the file for better naming convention.