What I'm about to describe is not what I really want to do. What I really want to do is much more complicated, but I have to start with a prototype so I can wrap my head around whatever is required.
I have a module that works fine. I can add a textfield or a group of radio buttons to the form that is displayed without a problem. I can add a button that says "MAKE IT SO!" without a problem.
However, I have no idea where to put code so that when the user clicks the "MAKE IT SO!" button my code gets executed. Let's assume, for now, that when the user clicks the "MAKE IT SO!" button, I want to increment a counter in a database field.
I know how to write code that executes an SQL statement to increase the value of a field by one.
I just need to know where to put it and what to name the function within which it is put!
Thanks
Comments
Here is what I think you need
From what you have said you have a function that should end with a line like
Where 'my_form_id' is the form id you gave the form. For example if your module is mymodule this would typically start with mymodule and some addition name/id. Your function to catch validation is then my_form_id_validate and the one to use for submitting the information my_form_id_submit. Both take two parameters, $form_id and $form_values like this
my_form_id_validate($form_id, $form_values) and ($form_id, $form_values). For more details you might want to look at http://api.drupal.org/api/4.7/file/developer/topics/forms_api.html.(Note this assumes you are not talking about content based on a node, in that case you would use the insert and change hooks)
Thanks...
I've got to run out, so I will try to look at this in the next few days. I may not be able to get to it immediately, though, and I wanted to thank you.
Got one thing working, but Is this the best way?
Well, I appreciate the offer of assistance, but I have nothing of the sort anywhere on my hard drive that I can find! Let’s use your assumption. That is, that the module in question is named mymodule.module. You are saying that I should find something in that module with the text “return drupal_get_form(‘mymodule_xxxx’,$form)”.
Nope.
Trust me, I’ve read the forms_api document about 1000 times but I can’t find anything in there that addresses the button issue.
Besides, as I think I indicated, I don’t want to put my code in either the submit function or the validate function. I want a completely separate function that when the user clicks on the form, that function runs and nothing else.
I’m not sure that I made myself clear as to the context of what my user is doing when the button shows up on the screen. Essentially, the user has a choice of two things that he/she can be doing. The two tabs are: view and edit. This assumes that the address is: mysite/node/xx. Let’s assume xx is 24 in this case, not that it matters.
I have disabled “view” (see one of my other messages which I posted recently) and the only thing that the user can do is “edit” the form. I use the forms_api code to put things like checkboxes and so forth on the screen and the user inputs the information that I’m looking for in the EDIT function, not the view function. Maybe that makes a difference.
In any event I can put a button on the form just like I can put a checkbox. And one of the buttons I can put on the form is “submit”. And it works fine. I want to put another button on the form and have only code that is executed when the user clicks that button be the code that I want executed.
I suppose I could hook onto the submit logic (assuming I could find it), but I think that would be horribly inefficient by the time I’m done.
Thanks for reading this far.
Here is what I’ve found. The prior incarnation of my module (the one I copied from … the multi-page example from the forms_api documentation) had a button on the form that was named “Back” as it was, of course, a multipage form. It looks like the function mymodule_form_alter($form_id, &$form) had the following code in it:
if ($_POST['op'] == t('Back')) {
$form['page']['#value']--;
}
This code, I think, just subtracts one from $form[‘page’]’s value if that is the button the user clicked.
I suppose I could go into hook_form_alter and test for the $_POST[‘op’] and if it is “MAKE IT SO!” then just not do anything else and just do what I want to do.
Something tells me that isn’t the “proper” way to do things although I’m thinking it might work.
Yup, I tested it and I have access at that point.
I’m wondering if there isn’t a more efficient place to put my code?
Thanks
It sounds like you are using the form hook
It sounds like you are using the form hook to create the fields in the form. That is normally used when adding content for a module that has content based on a node. (which this does not sound like). Do you care to post (or send me) the module so I can better try an answer your question?
I agree....
Here is the link to the page that contains the information about multipage forms:
http://drupal.org/node/54753
If you open the link tied to the text:
"so take a look at multipage_form_example_form in the fully functional multipage_form_example.module."
You get the page defined by this link:
http://cvs.drupal.org/viewcvs/drupal/contributions/docs/developer/examples/
If you open the link to "multipage_form_example.module", you get to this page:
http://cvs.drupal.org/viewcvs/drupal/contributions/docs/developer/exampl...
If you download the top element, you get to the text defined at:
http://cvs.drupal.org/viewcvs/*checkout*/drupal/contributions/docs/devel...
I am using that module (merely having changed the hook from "multipage_form_example" to "mymodule".
The rest of the changes I've made are all cosmetic and, since it is a test module, it looks pretty ugly at the moment. I could strip it down, if you want, but it still will look exactly like the "multipage_form_example.module".
I can add information that is collected during the "edit" process and I can add a button. I can, as I just indicated, intercept Drupal in the mymodule_form_alter($form_id, &$form) function.
The question that I have is whether there isn't some more efficient place to intercept the code, because the validate function is run when a "normal" submit button is executed and since the processing I want to accomplish is completely independent of Drupal I'd rather not have the validate function execute.
I'll send you my module if you really want it, but it won't give you anything that the multipage module does.
A step back
There are I think four general cases for forms/form fields in Drupal
1) A custom content type - form fields added with form hook
2) Add fields to an existing form - form_alter()
3) A completly custom form (something other than adding node based content)
4) Part of the setting for a module - settings hook.
Which one are you trying to do?
I'm not entirely sure......
Thanks for the information. That makes perfect sense to me.
The problem is that I'm experimenting and thinking of all the things I *CAN* potentially do with Drupal. And there are so many that my mind is spinning.
I started this particular line of thought by looking for ways to collect information that I could store in a database and recall, as needed. Based on your list of 4 things, that looks like #3. However, it isn't very far from being #1, either. That is, assume I collect this information from the node creator. Could it be that the whole purpose of the data is to generate back to the node owner/creator some specified content?
How aboout a car insurance company that uses the system to quote rates to prospective customers? They would then allow the user to create a node of type "insurance quote". In the process of establishing that node, they would ask for information like "make, model, # of miles driven per year, etc". Then the "view", once validated, would pop up with a quote. Or maybe the face page of the insurance company contract.
Now what I've asked about is extending the system even further. I've asked about doing "something" while collecting the information for the node, but specifically the "something" I want to do does NOT include processing the node's information, so I don't want to submit and I don't want to validate. In the context of my theoretical car insurance quote system, my module generates many potential different buttons, but only shows one at any one time to the user. Maybe that button, in case number one says: "Click here to register for our GREAT BIG PRIZE GIVEAWAY!" and the code behind that button merely puts that user's information into a database that I will later use for deciding who gets the prize. If the user doesn't click that button, nothing happens, even if the user successfully completes the car insurance quote. Do you see why I don't want this as part of the validation or submit process? And even if you can't, maybe you could come up with an example where you would?
In any event, my question is theoretical in the sense of asking:
How can I program the system to do something (anything) when a user clicks a button while not having the system do anything else?
I have now figured out how to do what I want, except for the fact that it goes hand in glove with the submit/validate process.
The question is whether there is a way to do it without invoking those routines.
My guess is that there are four answers, one associated with each option you mention. If so, that is great, as more flexibility is generally a good thing.
Thanks for hanging in there with me!
A few more thoughts
Given what you wrote case 1 or 3 would be the more likely choice.
In a general sense case 1 would used where you want to store the data and later view it. It also has the advantage that any functionality that works on node based content will work with (taxonomy, etc). Though normally the process of submitting node based content takes you the view screen you can with a little bit of code change that behaviour.
Case 3 is useful when what you want to do does not really fit the process of adding/editing node based content. Here the reasons and possibilites are pretty much endless. The drawback is you lose access to some functionality that is centered around node based content.
Well, I think we are on the same page....
so all that is left is for me to figure out where the best place to do what I want to do really is.
But you mentioned something that I've previously mentioned in another post:
http://drupal.org/node/67516
You said: "with a little bit of code change that behaviour." Would you mind taking a look at that post and see if you can help me there, too?
Back to here, though. Given that I've sort of decided to go with number 1 or number 3, can you let me know where you think the best place to put my code? If you know a link to a page of the documentation that talks about this, that would be fine.
At some point, I'll follow up about all four situations, but if you don't have time to address all four, I certainly understand.
Thanks again.
Are you still there?
Can you tell me whether where I've put the code is the proper placement?