formsapi question: saving additional fields' data
We can easily add fields to existing forms. Do we have a way to save their data? Example: I want to extend blocks' configuration pages. I can form_alter where $form_id = 'block_admin_configure', adding new fields. But block_admin_configure_submit() won't look for or save the added fields. To do so, I can only think of (a) (mis)using the #validate attribute to specify a function to pass the data to and save them there, or (b) hijacking the form by changing the form_id and sending it to a custom _submit function, which saves the custom fields and then invokes the original _submit function. Better approaches? Tips? Solutions that people have used elsewhere? Am I overlooking a relevant Forms API attribute? Thanks.
Nedjo Rogers wrote:
We can easily add fields to existing forms. Do we have a way to save their data?
Example: I want to extend blocks' configuration pages. I can form_alter where $form_id = 'block_admin_configure', adding new fields. But block_admin_configure_submit() won't look for or save the added fields.
To do so, I can only think of (a) (mis)using the #validate attribute to specify a function to pass the data to and save them there, or (b) hijacking the form by changing the form_id and sending it to a custom _submit function, which saves the custom fields and then invokes the original _submit function.
Better approaches? Tips? Solutions that people have used elsewhere? Am I overlooking a relevant Forms API attribute?
Thanks. You can add a #submit much like a #validate.
You can add a #submit much like a #validate.
Thanks Earl! Specifically (in case anyone else also missed this), you add an element to the $form's #submit attribute array, with the key being the function to send the form values to and the value being an array of arguments to use in addition to the default ones. Example: $form['#submit']['examplemodule_submit'] = array();
Hi, On Tue, 2006-04-25 at 14:39 -0700, Nedjo Rogers wrote:
You can add a #submit much like a #validate.
Thanks Earl!
Specifically (in case anyone else also missed this), you add an element to the $form's #submit attribute array, with the key being the function to send the form values to and the value being an array of arguments to use in addition to the default ones. Example:
$form['#submit']['examplemodule_submit'] = array();
Be aware that this will be the only hook_submit() that will be called. The default hook_submit() ($form_id .'_submit()') is only added when #submit is empty. I actually did a small tip about this yesterday. See http://heydon.com.au/node/926 for what I wrote. Gordon.
On 26 Apr 2006, at 1:45 AM, Gordon Heydon wrote:
Be aware that this will be the only hook_submit() that will be called. The default hook_submit() ($form_id .'_submit()') is only added when #submit is empty.
Umm. form_alter runs after it has set the default submit callback. [1] In his example, it would add the callback AFTER the default callback. In your example it would add it BEFORE. Your example could also have used : array_unshift($form['#submit'], array('function' => array()); ====== [1] http://cvs.drupal.org/viewcvs/drupal/drupal/includes/form.inc? annotate=1.109 Default submit callbacks are line 98 through 105 form_alter is called on line 111 -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com
On Wed, 2006-04-26 at 09:45 +1000, Gordon Heydon wrote:
Hi,
On Tue, 2006-04-25 at 14:39 -0700, Nedjo Rogers wrote:
You can add a #submit much like a #validate.
Thanks Earl!
Specifically (in case anyone else also missed this), you add an element to the $form's #submit attribute array, with the key being the function to send the form values to and the value being an array of arguments to use in addition to the default ones. Example:
$form['#submit']['examplemodule_submit'] = array();
Be aware that this will be the only hook_submit() that will be called. The default hook_submit() ($form_id .'_submit()') is only added when #submit is empty.
I actually did a small tip about this yesterday. See http://heydon.com.au/node/926 for what I wrote.
Gordon.
wow... much power, without calling all that module_implements stuff. FormAPI is pretty.
On 26 Apr 2006, at 2:27 AM, Darrel O'Pry wrote:
wow... much power, without calling all that module_implements stuff. FormAPI is pretty. there is a difference between pretty and powerful.
the difference is, it could be prettier. =) -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com
On Wed, 2006-04-26 at 02:30 +0200, Adrian Rossouw wrote:
On 26 Apr 2006, at 2:27 AM, Darrel O'Pry wrote:
wow... much power, without calling all that module_implements stuff. FormAPI is pretty. there is a difference between pretty and powerful.
the difference is, it could be prettier. =)
-- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com
Beauty is in the eye of the beholder. In this case I feel there is an elegant blend of form and function, not to say it couldn't be more refined, but it is still stinking pretty and if you disagree I'll sock you in the nose!!! ;) In comparison to the hook model where all the modules are scanned through testing if a particular callback exists, and not being able to say stuff like on don't use nodeapi from inline.module on these callbacks in a centralized place, beside whatever admin interface inline.module implements.... I think that specifying the processing for a content item and its life cycle is pretty nice, and much more streamlined. Admittedly in the long run some hooks will still be necessary, but I really like the road formAPI points down, even if we're only at the on-ramp.
On 26 Apr 2006, at 3:11 AM, Darrel O'Pry wrote:
In comparison to the hook model where all the modules are scanned through testing if a particular callback exists, and not being able to say stuff like on don't use nodeapi from inline.module on these callbacks in a centralized place, beside whatever admin interface inline.module implements.... Yeah. I think we need to give some serious love to our 'menu' system, and extract the important part (registering callbacks), into a more generic callback system.
I've been wracking my brain on just this, as I start turning the cogwheels for fapi 2.0. Fixing the callback system is one of the primary requirements before any serious work can begin on that. But anyway. enough idle chitchat. 4.7 isn't out yet. *eyes the warden to make sure he hasn't been noticed* -- Adrian Rossouw Drupal developer and Bryght Guy http://drupal.org | http://bryght.com
Gordon Heydon wrote:
On Tue, 2006-04-25 at 14:39 -0700, Nedjo Rogers wrote:
You can add a #submit much like a #validate. Thanks Earl!
Specifically (in case anyone else also missed this), you add an element to the $form's #submit attribute array, with the key being the function to send the form values to and the value being an array of arguments to use in addition to the default ones. Example:
$form['#submit']['examplemodule_submit'] = array();
Be aware that this will be the only hook_submit() that will be called. The default hook_submit() ($form_id .'_submit()') is only added when #submit is empty.
I actually did a small tip about this yesterday. See http://heydon.com.au/node/926 for what I wrote.
Gordon.
Ah. Interesting. I had no idea. I've never needed it, but these kinds of explanatory tips really need to be in the documentation. And I've added your blog to my RSS reader, Gordon. :-) ..chrisxj
participants (6)
-
Adrian Rossouw -
Chris Johnson -
Darrel O'Pry -
Earl Miles -
Gordon Heydon -
Nedjo Rogers