I would like to pass a parameter to a node page. This parameter will change the way the node page is displayed. I could use a URL like this:
http://hostname/node/1234?foo=bar
But, I don't see this done in any other modules. In fact, I don't see any other modules changing the display of nodes based on user-submitted parameters. So maybe this isn't the "Drupal way" of doing what I'm trying to do. The other problem with this approach is that if the user is not using clean URLs, this will not work (will need to append a '&' instead of a '?' to the URL).
So, what is the best way to modify the way a node page is displayed? I've implemented a solution using the $_SESSION variable, but this is such a basic page and it should not need to use the session.
- Jared
Comments
I forgot to mention that I
I forgot to mention that I know about arg(n), and I don't want to use it. There are other modules, like the one that handles comments, that can get confused if you put in numerical arguments as arg(2) . I.e., the following URL is bad:
http://hostname/node/1234/5678
where 5678 is the argument I want to pass.
I suspect that other arguments might confuse other modules. In fact, this seems to be a common theme in the problems I am having with Drupal; modules clashing. Besides, that style of argument passing induces some kind of structured path. I wouldn't want to use a URL like the following, because it indicated a hierarchy that doesn't exist:
http://hostname/node/1234/foo/bar
I was poking around and I
I was poking around and I found out that the tablesort code uses parameters (of course). It uses the l function to create a link:
<?phpl($title, $_GET['q'], NULL, 'sort='. $ts['sort'] .'&order='. urlencode($cell['data']) .'&'. $ts['query_string'], NULL, FALSE, TRUE);
?>
The l function handles appending the query arguments correctly, and the $_GET['q'] simply means create a link to the current page. This does exactly what I want!
query string or parameter
Are you writing a module, or just making a page with PHP?
For the first, many pages use "parameters"- they are part of the URL. Look, for example, at the node module- the nid is taken as a parameter. Also, if you define a callback for a particular URL the stuff after the defined path is passed as a parameter to the callback function. Look, for example, at the taxonomy module:
http://api.drupal.org/api/4.7/function/taxonomy_menu
http://api.drupal.org/api/4.7/function/taxonomy_term_page
If you are just doing a page, you can pass in parameters via a query string- note that the l() function can take a query string as a parameter, so this can be useful for constructing links from other parts of your site:
http://api.drupal.org/api/4.7/function/l
for example, if you make a link like this:
l(t('transaction'), 'node/100', array(), "transaction=55"))this will give you a URL like: www.example.com/node/100?transaction=55
Then within node/100 you could put PHP code like;
<?php
$trans = $_GET['transaction'];
print(check_plain($trans). "<br />");
if ($trans == '55') {
//do something
}
?>
Important note: if you do this sort of thing you must be careful to sanitize your input, or you will be vulnerable to SQL injection, XSS or other possible attacks. See: http://drupal.org/node/62304
---
Work: Acquia