Argument arg() support in Token
Summit - June 23, 2008 - 17:56
| Project: | Token |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Description
Hi,
I would like to use the arguments; $args[0]..$args[1]..etc..which also are used as views handling code within tokens.
Then it would be possible to have a node and refer within the node to the $args[0] etc.., right?
Thanks a lot in advance for considering this!
greetings,
Martijn

#1
$args[0] is just arg(0) right?
So, yeah, we should probably support those.
#2
How please...
I know that for argument handling code in view, you need to place $args[0] instead of arg(1), to get the arguments also working with panels. [ $args(0) = arg(1)]
Please tell me how token support arg(1) then please, and we will see how this goes further, ok?
Thanks in advance for sharing!
greetings,
Martijn
#3
Well, if the args are special things in Views then the hook_token_list and hook_token_values implementations for them should happen in Views itself.
#4
Uhh. $args is a special variable available in the views arg handling PHP snippet and not even related to token. This issue doesn't even make sense.
#5
Hi,
As Earl described above, $args is view-internal.
So please explain please how to get arg(0) support for token, which is not views-internal I asume?
Sorry for my mistake, but I am not a pro..
Thanks in advance for your reply!
greetings,
Martijn
#6
Ok.
There is no way to do it now. It would require additional code - probably in token.modules token_token_list and token_token_values.
However, the dynamic nature of arg() (i.e. the fact that you can have an arbitrary number of them) means that this should probably be implemented with the whole "token 2" implementation.
If someone wanted to make a patch for arg0, arg1, arg2, and arg3 that would work for the most common cases up until that point I would be happy to commit that.
#7
Summit:
Please read the API.txt file that comes with token module. it explains how you can expose more valeus to token. one such value ofcourse can be arg(0) or arg(n).
dont open this issue again, if you have questions about how to create a module to expose those values you can ask that on the forum.
good luck.
#8
@litwol - I agree with most of what you said, but I think arg's are at least somewhat reasonable tokens to expose for use and the proper place for such a global token would be in token itself.
#9
Id sure like to see this as a 5.x feature. Or at lease SOME way to point the current uid ($arg(1) to the path of a menu item)... I have been beating my head on this one for days, and the only solution I have heard is the best way is to use token, but how can I use token if it doesn't support arguments.
Kostly
#10
So I've been able to add tokens arg0, arg1, arg2 and arg3 by adding them in the token_node.inc file. What I really wanted to do was add them to a new token_arg.inc file. I did add require_once("$path/token_url.inc"); to token.module, but the new tokens neither show up in a list of available tokens, nor do they work. In the token_node.inc file they show up in the list, and, more importantly, they work.
I'm sure I'm just missing some little step, but even reviewing other modules and how they define tokens hasn't enlightened me.
Any suggestions?
Thanks.
P.S. I'd rather see something like [arg:0] or [arg(0)] or some such thing that would allow an argument to be passed with the token. I gather from one of the comments that such a thing might be coming in a new version of token?
#11
@colin_young - if you post the non-working code as a patch then we can discuss why it isn't working.
And...while [arg:0] are a good idea it's probably not in the timeframe for adding this patch to this module.
#12
@greggles - Sure. Attaching the files here. (Is there some way to create a patch containing multiple files without CVS? I really should install some sort of revision control -- or just start using my subversion installation :) -- but I really hadn't been planning on mucking about in drupal code.)
My comment about a future version supporting arg:0 was prompted by your comment about a "token 2" module, which, I'm guessing, is way off in the future. I assume that arg0, arg1, etc. is a hack that would be retired if/when a token 2 module was released.
#13
Hi,
I installed this patch..but I do not see the arguments as tokens, sorry.
I tried bringing them on board token_node.inc, but didn't succeeded either.
I read your remarks:
So I've been able to add tokens arg0, arg1, arg2 and arg3 by adding them in the token_node.inc file
Do you have may be your token_node.inc solution for me?
In your remarks you refer to token_arg.inc (I do not see this in the patch).
Of course also very much interested in the best solution to be able to use the url-arguments as tokens in nodes.
Thanks a lot for your reply!
greetings,
Martijn
#14
In fact, these are probably global tokens that should be added to token.module's hook_token_list and hook_token_values if added anywhere. That should eliminate the include problems.
#15
@Summit - token_arg.inc was a file name I started with before I realized it should be called token_url, or as greggles suggests, put into a more appropriate existing token file. I'm going to make those changes now and submit a new patch.
#16
@greggles - took your suggestion and trying again.
#17
Hi,
Tokens fot argument are working now with this patch.
The only problem is with aliased url's still.
So my url www.example.com/Nepal/Kathmandu/flight is "inside drupal" www.example.com/taxonomy/term/4
So as token-value for arg2 = 4 and not flight..
greetings,
Martijn
#18
To handle aliases, you can use drupal_get_path_alias($_GET['q']) and then run the returned path through arg to get the components.
This patch provides [argX] and [argX-alias] tokens, where X can be 0, 1, 2, ... up to a defined value (so you can have as many args as you want, but I left the maximum at 4 as in colin_young's patch).
#19
updating status to reflect the new patches in #16 and #18
#20
subscribing
#21
Patch works for me. Thanks for all your work on this, it's an excellent addition!
Jen
#22
Here's a new version against the token starter kit. Otherwise it is copy and paste and untested.
@MGN, @jenlampton - Thanks for your work. Drupal's coding standard is generally not to use inline comments and to make comments full sentences with a capital letter and a period on the end, so not quite RTBC ;)
#23
Thanks greggles. I think the starter kit is a good idea and a good place for these tokens.
It looks like you copied and pasted the code snippets into the wrong functions (swapping token_list and token_values). Here is a new patch that fixes this problem.
#24
This patch is a critical part of a module i'm writing called Custom Reports (http://drupal.org/project/customreports) - subscribing.
#25
@a_c_m, does this mean that you have tested the patch in #23 and can confirm that it is working?
#26
Not quite.
patching file tokenSTARTER.moduleHunk #1 FAILED at 1.
Hunk #2 FAILED at 20.
Hunk #3 succeeded at 68 (offset 16 lines).
2 out of 3 hunks FAILED -- saving rejects to file tokenSTARTER.module.rej
But the code itself worked fine. I would quite like to see it not need TOKEN_NUM_ARG_TOKENS and instead just work out how many args there are in the current request and use them.
Perhaps also allow for named arguments e.g. /node/123?name=frank then being able to use [arg-name] etc etc
I've attached the patch re-rolled to apply cleanly to the latest revision.
#27
Agreed. Marking needs work for that.
So, [arg-name] would contain "frank" ? That seems like a query string parameter, not an "arg" and therefore a different feature request. I'm also not sure that belongs in the STARTERKIT.
#28
I agree with greggles on the query - its not part of the url...
Here is a leaner patch that doesn't require that constant.
#29
Tested MGN's patch (#28) and it works as expected.
#30
I tested patch in #28 and it worked as expected, tests conduced under E_ALL and no notices/errors were generated. i used automatic node titles module to test this.
token used: [arg0]-[arg1]-[arg2]-[arg3]
node title generated: node-2-edit-[arg3]
#31
Missed to change status.
#32
Would not it better if the code could replace "[arg-3]" with an empty string, if the argument does not exist? If I call
arg(3)in such case, I would get a NULL value.#33
I now agree with Kiam, but I'm unsure how to do this. How far up through args should we go getting null values? To 10?
#34
To do what Kiam suggests would require defining some arbitrary constant to do a null token replacement, which greggles and a_c_m didn't seem to like. There will probably be a slight performance hit, but probably insignificant. Its easy enough to do.
Just tell me what arbitrary number of arg tokens we want to null out, and I can create a new patch.
Also, it seems the default is for tokens to go untranslated if there is no replacement value. Sometimes this results in an annoying [token] popping up where you would prefer to have nothing at all.
So perhaps its worth considering if token_replace (et al.) should be altered to automatically filter out any untranslated tokens?
#35
oops. cross posted with greggles.
#36
My feeling here is that hook_token_values modules should default to '' and then put in the value when necessary. The reason being:
1. I don't want to make token replacement any more complex than it has to be for performance reasons.
2. There are legitimate reasons to use text like "[something]" and want it to not be replaced with an empty string.
I am very sorry to have changed my mind on this issue - @MGN - are you up for one final re-roll? Thanks!
#37
I agree. I realized after I made the suggestion that other modules also use square brackets as a signature and nulling out [something] would certainly interfere with those modules. Anyway, I am happy to do as many rerolls as it takes - not a problem. Its better to get it right.
Since its in tokenSTARTER and designed to be modified, the actual value I assign to TOKEN_NUM_ARG_TOKENS doesn't really matter, though it would be best if it worked as expected in most circumstances. I think 10 is a fine number.
Are we settled on nulling out [arg-N] for unset arguments through [arg-10] ?
#38
What I was thinking of is the Drupal core code that passes
$argtohook_help(); if I remember well, that argument is an array with X elements, which by default are empty strings.The called function executes the following code:
<?phpfunction drupal_help_arg($arg = array()) {
// Note - the number of empty elements should be > MENU_MAX_PARTS.
return $arg + array('', '', '', '', '', '', '', '', '', '', '', '');
}
?>
#39
I think there is already a constant for the maximum number of parts a menu string should be made of; it should be also the maximum value accepted by
arg().EDIT:
arg()doesn't set a limit for the$indexargument it accepts, but it's more probable that passing a value higher thanMENU_MAX_PARTSthe function doesn't return a value at all.#40
Yes, it seems our constant can be based on MENU_MAX_PARTS.
#41
Here is a patch that creates a fixed number of arg tokens, based on the value of MENU_MAX_PARTS. All [argN] and [argN-alias] tokens will return null when arg itself returns null.
Still, I am not completely convinced that basing the number of tokens on MENU_MAX_PARTS make sense.
As Kiam points out, arg() itself doesn't set a limit. And I have verified that you can use arg() to retrieve as many parts of the url as you like (I tested up to 10) for use in a module callback page. Its true that only the first MENU_MAX_PARTS of the path will be used in identifying the menu item in the menu_router table. But that doesn't mean that module developers won't use arg() directly and exceed this limit.
Then again, MENU_MAX_PARTS is defined as 7, and this should cover most cases just fine.
#42
This is a one year old issue. I am happy people are working on this and I would love to see this feature but maybe we have gone about this the wrong way.
Maybe we need to first include the ability to add variables to people who create tokens? For instance, I may want to create a token [comment-42]. I think doing this would ultimately solve a multitude of issues (and ultimately close this ticket).
#43
@MGParisi. I am not sure if I understand what are looking for, but using the tokenSTARTER module, you can create your own custom tokens.
This doesn't seem to be related to the current issue, so you might think about opening a new issue if you want to develop the idea further.
I am interested in "closing this ticket" however.
A couple of months ago greggles asked for the re-roll in #41. Anyone willing to test it so we can move to RTBC?
#44
I have used the patch in #41 to solve an issue I had with Custom Breadcrumbs, as can be seen here: Taxonomy breadcrumb with the taxonomy view. So I can confirm this works.