I have a menu item whose callback is drupal_get_form, but in the form function, I would like to know what arguments follow my path. What I mean is that my menu path is:
custommod
and the actual URL I am calling is
custommod/3
and in the form I want to be able to see that 3. Is this available in the API? I tried making my callback a custom function and then using drupal_render to render my form at the end of that function, and while then I see the 3, the form came out all messed up--it was out of order it seems. Is this the right way to do it? Or is there something I am missing?
Thanks!
I usually just get it using a call to arg(1) in the from rendering function.
Make sense?
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Fred Jones Sent: Wednesday, July 30, 2008 11:05 AM To: support@drupal.org Subject: [support] Simple Module Coding Question
I have a menu item whose callback is drupal_get_form, but in the form function, I would like to know what arguments follow my path. What I mean is that my menu path is:
custommod
and the actual URL I am calling is
custommod/3
and in the form I want to be able to see that 3. Is this available in the API? I tried making my callback a custom function and then using drupal_render to render my form at the end of that function, and while then I see the 3, the form came out all messed up--it was out of order it seems. Is this the right way to do it? Or is there something I am missing?
Thanks! -- [ Drupal support list | http://lists.drupal.org/ ]
On Wed, Jul 30, 2008 at 12:07 PM, Metzler, David metzlerd@evergreen.edu wrote:
I usually just get it using a call to arg(1) in the from rendering function.
Make sense?
This works as long as your form is always on that exact URL. An alternative, if you want your form to be more flexible, is to use arg(1) as a 'page argument' in the hook_menu definition and then add it as a variable in your custommod_form_name($var_here) function declaration.
This works as long as your form is always on that exact URL. An alternative, if you want your form to be more flexible, is to use arg(1) as a 'page argument' in the hook_menu definition and then add it as a variable in your custommod_form_name($var_here) function declaration.
Brilliant--you answered my question just before I wrote it. I see that if I use:
array('addciviparticipant_form',arg(1)),
for my 'callback arguments' then it indeed works as you say. But in fact just
array('addciviparticipant_form'),
also works. :)
The only one that FAILS is:
'addciviparticipant_form'
Thank you!!
Er, but that doesn't work in drupal 5 or later, does it?
I had to do some significant reworking of forms that were built that way when I updated to D5.
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Greg Knaddison - GVS Sent: Wednesday, July 30, 2008 11:19 AM To: support@drupal.org Subject: Re: [support] Simple Module Coding Question
On Wed, Jul 30, 2008 at 12:07 PM, Metzler, David metzlerd@evergreen.edu wrote:
I usually just get it using a call to arg(1) in the from rendering function.
Make sense?
This works as long as your form is always on that exact URL. An alternative, if you want your form to be more flexible, is to use arg(1) as a 'page argument' in the hook_menu definition and then add it as a variable in your custommod_form_name($var_here) function declaration.
-- Greg Knaddison Denver, CO | http://knaddison.com | 303-800-5623 Growing Venture Solutions, LLC | http://growingventuresolutions.com -- [ Drupal support list | http://lists.drupal.org/ ]
Oh, right. I was so carried away looking into drupal_render_form etc. on the API site that I forgot about that--I saw args() used over there and I tried that and it failed. :)
Thanks!
But actually custommod/3 doesn't seem to render my form. If the callback is a custom form, it works, but if it's drupal_get_form, it just shows the title and that's it.
On Wed, Jul 30, 2008 at 8:07 PM, Metzler, David metzlerd@evergreen.edu wrote:
I usually just get it using a call to arg(1) in the from rendering function.
Make sense?
On Wed, 30 Jul 2008 11:07:43 -0700 "Metzler, David" metzlerd@evergreen.edu wrote:
I usually just get it using a call to arg(1) in the from rendering function.
Make sense?
This makes your form definition function dependent on where it is rendered.
What about ... 'path' => 'somepath/'.arg(1).'/'.arg(2), 'callback' => 'drupal_get_form', 'callback arguments' => Array('my_form',arg(1), arg(2)), ...
function my_form($p1, $p2, $form_values) { $form['bau']=array(...); }
I just had issues with multistep forms making use of $form_values when I was willing to provide a fall back from somepath/1/2 to somepath/ without duplicating too much code. I solved it in a way that I didn't find intuitive but I forgot how I did.
But in less complicated situation it definitively works smoothly in D5.
Thanks for this guys I really didn't realize this was possible. When I read the module porting guidelines, it suggested that form rendering functions now only take one parameter, that is formid. I wish I knew about this before I wrote all those static caching functions as a workaround. :).
FYI: If you do the exact code Ivan suggests here this, you need to make sure it's in the (!$maycache) section of the hook_menu, which means of course you can't customize it. This will be better in D6 where wildcards take up the slack.
Dave
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Ivan Sergio Borgonovo Sent: Wednesday, July 30, 2008 11:41 AM To: support@drupal.org Subject: Re: [support] Simple Module Coding Question
On Wed, 30 Jul 2008 11:07:43 -0700 "Metzler, David" metzlerd@evergreen.edu wrote:
I usually just get it using a call to arg(1) in the from rendering function.
Make sense?
This makes your form definition function dependent on where it is rendered.
What about ... 'path' => 'somepath/'.arg(1).'/'.arg(2), 'callback' => 'drupal_get_form', 'callback arguments' => Array('my_form',arg(1), arg(2)), ...
function my_form($p1, $p2, $form_values) { $form['bau']=array(...); }
I just had issues with multistep forms making use of $form_values when I was willing to provide a fall back from somepath/1/2 to somepath/ without duplicating too much code. I solved it in a way that I didn't find intuitive but I forgot how I did.
But in less complicated situation it definitively works smoothly in D5.
-- Ivan Sergio Borgonovo http://www.webthatworks.it
-- [ Drupal support list | http://lists.drupal.org/ ]
I usually just get it using a call to arg(1) in the from rendering function.
This makes your form definition function dependent on where it is rendered.
What about ... 'path' => 'somepath/'.arg(1).'/'.arg(2), 'callback' => 'drupal_get_form', 'callback arguments' => Array('my_form',arg(1), arg(2)),
I don't understand you, Ivan. Using:
'path' => 'somepath',
and then calling arg(1) will yes only work at a URL of the form:
somepath/x
but your solution also only will work at a URL of the form:
somepath/x/y
I don't see any difference--on the contrary, using arg(1) etc. seems more flexible, because then I can always access arg(3) without worrying about adding to my callback arguments.
Perhaps I am not understanding you correctly.
Thanks
On Wed, 30 Jul 2008 20:58:31 +0200 "Fred Jones" fredthejonester@gmail.com wrote:
What about ... 'path' => 'somepath/'.arg(1).'/'.arg(2), 'callback' => 'drupal_get_form', 'callback arguments' => Array('my_form',arg(1), arg(2)),
I don't understand you, Ivan. Using:
'path' => 'somepath',
and then calling arg(1) will yes only work at a URL of the form:
somepath/x
but your solution also only will work at a URL of the form:
somepath/x/y
In the hook yes... but at least you can reuse the form generating function elsewhere.
Specifying the path with arguments in the menu_hook is a form of "input validation". If you don't need it... or you'd like to rely on default param etc... you could simply:
'path' => 'somepath', 'callback' => 'drupal_get_form', 'callback arguments' => Array('my_form',arg(1), arg(2)),
...
but still you could use my_form elsewhere... maybe in another hook or as part of a more complicated page or merged with other forms of modified through form_alter etc...
To be extraclear
function my_form($form_values) { $mystuff=(int)arg(1)*(int)arg(2); }
is much less flexible than
function my_form($a, $b, $form_values) { $mystuff=$a*$b; }
and you could even wrap input validation elsewhere.
To be extraclear
function my_form($form_values) { $mystuff=(int)arg(1)*(int)arg(2); }
is much less flexible than
function my_form($a, $b, $form_values) { $mystuff=$a*$b; }
and you could even wrap input validation elsewhere.
Ah, now I see. Yes, you are quite right--good point.
Thanks.