Posted by macelee on December 13, 2012 at 1:36pm
Drupal 7 site, I have a custom module containing dynamic pages using query strings, such as: "page?id=123". In my code I need to parse the query string (using functions drupal_get_query_parameters, then getting the 'id' value from the returned array), I then use the 'id' value in a database query.
If a user decides to log in from that page, then after the redirection the query string changes, to something like "page?destination=page%3Fid%3D123". I can of course parse the 'destination' string to get the 'id' value? But that would involve quite some coding and I have to repeat doing this for multiple pages. I wonder if there is any better practice?
Comments
My Solution is here for getting correct nid of node
Hi,
I have read your comment but not exactly sure about following point :
Why you are not using clean url. This is main feature of drupal, also helpful from SEO purpose. I will suggest you to use CLEAN URLs if possible.
And regarding getting correct id, you can use arg() function in your code. This function always return array with correct nid of page, whether you are using CLEAN URLs or not.
I believe this function will help you.
Pushpinder Rana
hi er.pushpinderranaThanks
hi er.pushpinderrana
Thanks for your reply. I am still developing my custom module and have not got a chance to look into clean URL.
Currently I have to use the following code the handle query strings:
<?php$query_parameters = drupal_get_query_parameters();
if (array_key_exists('destination', $query_parameters)) {
$dest = $query_parameters['destination']; // the destination string such as "path/to/page?a=123&b=456"
$pos = strpos($dest, '?') + 1; // the position of '?' in the query string
$query_string = substr($dest, $pos); // remove everything before (including) '?'
$keys = explode('&', $query_string); // a list of key/value pairs in the query string
// further parsing the pairs, more code omitted
}
?>
As you can see it takes a lot of effort to extract the values (123, 456 in my example above). Query string is such a fundamental element in dynamic web programming, it would be quite a surprise if a good content management system like Drupal cannot support this in a way that is significantly simpler that what I have coded. I must have missed some simple solutions.
You probably won't find it
You probably won't find it surprising to find that you are going about things in completely the wrong way :-)
Yes, Drupal has a full system for managing URL arguments. If you look at any of core or any of contrib you'll see that ALL of them use elements of the path as their arguments. It's /node/7 , not page?node=7
You need to use your hook_menu to declare that you expect arguments in the incoming request, and then deal with those arguments in your page callback.
This is documented at hook_menu
And further explained in page_example_menu() found in the Examples for developers
If you use the menu router normally, I think you'll find that the ?destination= redirect should wourk as it normally does for other pages.
You really shouldn't have to be decoding the URL parameters like that, and when you find yourself doing so, it's a sign that there will be a better way of doing whatever you are doing. It may need a re-think
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thanks! That is exactly the
Thanks! That is exactly the kind of information I am looking for.
The pages are being ported to Drupal from an old PHP site so the intention is to reuse as many old code as possible. I will start converting them to the proper Drupal way.