I have a situation where a link leads to a dummy page who's only purpose is to execute a rule. So when the link is clicked the rule is WHEN CONTENT IS GOING TO BE VIEW -> ADD USER ROLE.
That rules work fine and the user is left at the dummy page. The dummy page has no content and I want to redirect the user back to the page they were referred by so that they see it with their new role in place because it will now have different features.
How can I Page Redirect back to the page the link was originally clicked on. It is basically a turn on/turn off feature and I need the page with the link on it refreshed but I cannot find a way to have rules bring me back to the referring page.
Any suggestions are appreciated.
Thanks.
Comments
Left Page Redirect blank and added destination
There is probably a better way than this but here is what I ended up doing.
When I link to the dummy page I user 'print drupal_get_destination()' so it looks like example.com/dummypage?destination=currentpage
Then in rules I set my last action to Page Redirect and left it blank. Now when that rule fires it is using the destination from the url and seems to work just fine.
Two suggestions
A pretty innovative solution! It has some limitations, but the same is true for all solutions and you managed to get a working solution with the tools you had present. Well done!
The two drawbacks I see are:
* You use a rule triggered on 'Content is going to be viewed', which is an event occuring more or less all the time. Thus, your solution will have performance consequences on your site (even if they may be small).
* The redirection back seems pretty fragile – that it works at all could be considered a bug. That means that it might break when you install updates.
Still, it is not bad.
I have two or three different suggestions, if you're interested:
-1-
If your rule trigger on a node page, you could redirect to '/node/NN/special-effect' instead of '/dummypage'. You could have the rule still triggering on content view, but run a check on
arg(2) == 'special-effect'to see if it should be executed or not. When the rule is finished, the node module will take care of the path and render the node just as usual. If you don't like the path to be changed, you can add a redirect to '/node/NN' as last step in the rule – and simply fetch the NN from arg(1).-2-
Rather than redirecting to '/dummypage', you can redirect to '/dummypage/NN' and have the rule redirecting back to 'node/NN' by reading from arg(1) as in the example above.
-3-
You could try out the Button Field module and associate a rule set with it. My experiences with that module are not all good, but it could be a good fit for this purpose.
Good luck!
//Johan Falk, NodeOne, Sweden
Thank you. I am quite
Thank you. I am quite interested in your suggestions as I was concerned as well that my method may be a bug.
I have a few things I would like to try clear up though, the first is that the link to dummy page can be click from any page on my site (nodes & views) so the url could be anywhere from / to /1/2/3/4/5/6/7/8/...
I read the arg page you linked to and would love to try that route but I know little about it and am concerned that it may not work if I do not know the number of arguments there could be. I also read in the comments though that $args = arg(); could be used which seems like it would use all arguments and that the number of arguments would not matter. That would leave me with the issue of removing dummypage from the argument.
Anyway, is your method in suggestion 2 something that could work in my situation if dummypage were changed to a view so that dummypage/1/2/3/4/5 would not pull up a 404. The reason I ask is I have no idea how to use arg() and would have to learn it first and would hate to spend all that time trying to figure it out just to realize it can not be done.
Thank you so much for your time and suggestions.
Method 2 should work
I think method 2 could work even for non-node pages, as you say.
You could try the following:
dummypage/*. Using this module avoids custom PHP in a trigger for 'User is going to view a page', which would be slowing down your site on every page view.<?phpecho implode('/', array_shift(arg()));
?>
The arg() function is pretty handy, and does the following:
* Returns the parts of the system path in an array.
* If called with an argument, say arg(2), it returns a string with that part only (starting from zero).
The expression in step 3 above uses
array_shift()to knock off the first part of the array ('dummypage'), and thenimplode()to glue together the remaining pieces with a '/' in between them. It returns a string, which is the address to which you want to redirect.Good luck!
//Johan Falk, NodeOne, Sweden
Thanks for the info again you
Thanks for the info again you have been extremely helpful and I will be trying what you suggested today. I have one question though as you have pointed this out twice, and I therefor suspect I am doing something wrong when using "content is going to be viewed" but I see no other way in the drop down options that my rules could be triggered. It also seems that you are saying triggering a rule when content is going to be viewed will slow down every page, not just the pages that are triggering rule, is that the case and if so what is the alternative.
Thank you again.
Tricky with alternatives
I completely missed this first question. Sorry!
It is difficult to find alternatives to the general trigger 'user is going to view a page', if you want to fire off a rule from a non-node page. (For nodes you can run 'content is going to be viewed', but that is only a little bit better, since most page views most likely are node pages anyway.)
What you want to make sure with rules firing on these events is that the conditions are as lightweight as possible. It is also wise to evaluate cheap conditions first, to avoid the expensive ones if the earlier evaluate to FALSE.
An alternative that I've been thinking about is the possibility to hook a rule set directly into the page request system, just as the Page manager in CTools does. It would be neat with a plugin that allows rules to be fired off when a particular URL is visited, without having it stand on guard for every page load. (I think I'll write a feature request for that right away!)
Good luck!
//Johan Falk, NodeOne, Sweden
I am very sorry to have to
I am very sorry to have to bother you guys again but when I try the code above
<?phpecho implode('/', array_shift(arg()));
?>
I get this error
warning: implode() [function.implode]: Invalid arguments passed in /home/.../modules/rules/rules/modules/php.rules.inc(89) : eval()'d code on line 1.
I have looked around for what is wrong but I am afraid that with my limited knowledge of php I am not even sure what to search for.
Where do you use the function?
In which field do you put the function? Does that field expect the
<?php
?>
Does it expect a return statement, or does it expect a string with a path that it should redirect to?
I'm not very familiar with the page redirect action, but a screenshot of the settings you're using would probably help.
Don't give up!
//Johan Falk, NodeOne, Sweden
Sorry for the delay, I was
Sorry for the delay, I was out of town and do not worry I will not give up. I have linked to a Screen Shot that shows the form I am using for the Page Redirect rule and yes rules is expecting the php tags. I tried the same code:
<?phpecho implode('/', array_shift(arg()));
?>
in a template file and got a similar error
•warning: implode() [function.implode]: Invalid arguments passed in /home/.../node-content.tpl.php on line 2.
Is it possible there is something wrong with the code because when I change implode to explode in either my template or the screen shot attached, in both situation, the template and rules module return "Array".
Thank you for your time and patience
Does the silence mean what I
Does the silence mean what I am trying to do is not possible? I have been search but still can not find a solution. It may be that I am not sure what I should be searching for though so any suggestions are appreciated.
Thank you.
Perhaps arg() not set?
I don't know when in the process the event you're triggering on is fired, but I guess it is possible that it is fired before the arg() is set. If so, the implode function would get some weird input and answer with an error message of the kind you get.
I would suggest running
dpm(arg());orprint_r(arg());inside the redirect field. (You could prepend it to the existing statement.)I am not 100% sure that the redirect textfield accepts PHP code. The list of available objects is no guarantee of this (but the error message you get clearly indicates that the PHP is being executed in some way).
Those are my two entries for bug hunting. Good luck!
//Johan Falk, NodeOne, Sweden
PS: Nothing is impossible! (Kind of.) The silence from me only means that I've been insanely busy the last ten days.
I rethought the whole thing
I rethought the whole thing and changed the way I was going about it. I can just structure my links so that it looks like http://example.com/dummypage?target=current/location. Then for my rule all I have to do is redirecting to
<?php echo $_GET["target"]; ?>.Seems to work so far. I can imagine you have been quite busy as the more I read about drupal the more I find contributions you have made including some videos that have been quite helpful so thank you for the help here and all you contribute elsewhere as well, it is appreciated.
Add a special node with "Full
Add a special node with "Full HTML" or "PHP code" body.
Edit body, add JS code "history.back(-2);".
Redirect to this page.