By NiklasBr on
I'd like to load a form and immediately trigger its submit function inside the PHP code, i.e. without having to print it to the user and having the user manually press the submit function. Is this possible?
I'd like to load a form and immediately trigger its submit function inside the PHP code, i.e. without having to print it to the user and having the user manually press the submit function. Is this possible?
Comments
Drupal 6: drupal_execute()
Drupal 6: drupal_execute()
Durpal 7: drupal_form_submit()
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Thanks!
Thanks!
Have I understood the documentation correctly that I can do this and have the extra $form['stuff']; processed in the drupal_execute() function?
Edit: What I am trying to say is that it seems to be causing an infinite loop.
That's because drupal_execute
That's because drupal_execute() builds the form, which calls hook_form_alter() which calls drupal_execute() which builds the form, which calls hook_form_alter()...
What are you trying to do? Tell us in non-programming terms. Don't use terms like 'submit', 'build the form', 'execute' etc. Use plain English as if you were explaining it to a non-coder.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
When the user goes to node
When the user goes to node/add/the-node-type-in-question I want to have the form submitted in my module immediately and have the user land on node/[new_node_id].
As far as I can tell it is a regular node form "the_form_id_I_want_to_submit". I have added a few extra fields to through my own module which uses the hook_form_alter() function.
Why do you want to do this?
Why do you want to do this?
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Business reasons.
Business reasons.
The user should not need to wait for the form to transfer over the network and render on the screen and the user should not need to find the submit button and manually click it. Why? Because the form already contains all the values properly selected and filled. I want to automate some stuff that humans don't need to do because I presume machines can do them.
I tried doing it by calling .submit() using jQuery and it worked, but I rather want do it even earlier because JS is a bit finicky and it is unnecessary to do all those network requests and wasting user's battery life and time.
To elaborate on my question,
To elaborate on my question, forms are for receiving user input and submitting it to the server. Since you are trying to have the form submit without any user input, it doesn't really make sense to even use a form. So I'm trying to get a feel for what you are trying to do, so I can recommend a better way to do it. You have explained how you are trying to do something (you are trying to set a form to automatically submit), but not why you are trying to do that, or what your overall goal is.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
My goal is:
My goal is:
1. To save user time.
2. To save network and battery resources for the user.
3. To save money in the business perspective because user time equals company money.
4. To lessen user frustration because they have to wait less to have the form submitted automatically for them.
I hope that is clearer. How do you propose I submit the form?
I propose you don't use a
I propose you don't use a form at all, since you are not getting any user input, which is the purpose of a form, and therefore you do not even need a form.
Unfortunately that doesn't seem to satisfy you, and you don't seem to want to elaborate on what your goal is, so I'm just going to leave it at that. Maybe someone else can help you do whatever it is that you are trying to do.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Not using a form is not an
Not using a form is not an alternative because the form is provided by Drupal Core. Unless you are somehow suggesting that I modify Drupal core (which sounds very very odd).
Why can't you accept that I need to submit the form? What's wrong? Why are you so rude?
I apologize for having worded
I apologize for having worded my posts in a manner that has led you to think I am being rude. It was not my intention. And I am not suggesting you alter core, as I would never recommend this to anyone. However there are 10 ways to do anything in Drupal, and nevets has given some examples.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
I agree with Jaypan, I
I agree with Jaypan, I suspect you do not even need/want a form. If you are somehow auto filling the form you likely do not even need it. For example for nodes/content there is node_save() and for users there is user_save(). The point is many core forms are simply a human interface to an api. If you do not need the form, the api would be more appropriate.
I just want to auto-submit
I just want to auto-submit the form. I don't want to get into a philosophical discussion whether or not drupal_execute() should be allowed to exist or not because you two think that humans must always submit forms!
Forms are for receiving user
Forms are for receiving user input. If you are not receiving user input, then you do not need a form, and there is a better way to do whatever it is that you are trying to do. It's not an opinion, it's a statement of fact. It's like if someone was trying to ask us how to use a car as a skateboard. We could explain how to stand on the roof, and push off like a skateboard, but you'd be better off using a skateboard than a car.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
I don't necessarily disagree
I don't necessarily disagree with you. Why does even drupal_execute() exists if users should ALWAYS submit forms?
_
make that three. As well as many others:
http://en.wikipedia.org/wiki/HTML_forms
http://www.w3schools.com/tags/tag_form.asp
http://www.webopedia.com/TERM/F/form.html
http://www.tutorialspoint.com/html/html_forms.htm
http://htmldog.com/guides/html/beginner/forms/
Reading those, and you can google many many others, you'll see a trend... 'user input'... that's the purpose of forms on web pages. It's not like there's even a debate, lol.
As jay has already stated multiple times, if you're not collecting user input there are better and 'proper' ways to define and submit information.
I don't necessarily disagree
I don't necessarily disagree with you. Why does even drupal_execute() exists if users should ALWAYS submit forms?
Off the top of my head, I can
Off the top of my head, I can think of two scenarios where it can be used:
1) I've created a custom form, then used the data from that form to programmatically submit the core form, to save the data using the core APIs. For example, I've created a form that takes input from the user about a business, and for anonymous users also provided a username and email field. I then used the username and email field to create a user by programmatically submitting the user register form, even though the actual user_register_form() was never used.
2) Data can be pulled from a 3rd party service in cron runs, then that data can be submitted as if it were using a form, even though the input has not come from user input at all.
That said, for both of these cases, I'd still use a different solution. While drupal_execute() exists, there are even better solutions than it. But they still do not require a form.
And to add to that, you are getting hung up on the point of the form - you don't need a form to do whatever it is you are trying to do. There is some other way to do it, but since you haven't told us what you want to do, we cannot advise you on how to do it.
I know you think we are being difficult here, but we are simply offering free, volunteer help (even after you've called me rude). On the contrary, even though we want to help, you are not giving us the information to be able to help with, so from our perspective, you are the one who is being difficult.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Sure, here goes:
Sure, here goes:
Do please tell me how I replace the node creation form with some other functionality that incorporates the following modules:
Date to auto-fill content depending on the current user.
Auto node-title to automatically create a node title depending on the current time, location, and user.
Organic Croups so that the node is created into the current user's group.
A geo-location module that gets the users current IP-based location at the moment the enter the node/add/node-type form and creates a custom field with that content.
A custom node-form validation modules that keep the content secure.
A custom module that tracks referrer and fills the node body field depending on the content of said referral.
All the above modules hook into the node form to pre-fill the content. Please advise me how I can replace these modules rather than use drupal_execute()!?
The question here is whether
The question here is whether or not you even need a node. But I still don't know because you still haven't told us what it is you are trying to do.
I give up. I'm going to bed.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
1. If you are able to assume
1. If you are able to assume things, please for the love of everything please assume that I need a node!
2. If you want to answer the question "can I automatically submit a node/ass/nodetype form", please answer it or stop being rude!
1. If you are able to assume
I'm not going to assume that, because it's not clear of if you need a node or not. Please trust me that I have a pretty good understanding of Drupal. I've contributed to core, and I've contributed to this site you are using, and I've got a portfolio of Drupal pieces if you'd like to look to see that I know what I'm talking about.
But to be honest, I'm tired of you calling me rude for just trying to help. In my books, that is rude. I think it will be less frustration for both of us if I just stop interacting with you.
Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
Here is the approach I tend
Here is the approach I tend to use.
Temporarily implement hook_node_insert() and use either
dpm($node)ordrupal_set_message('<pre>' . print_r($node, TRUE) . '</pre>');if you do not have devel installed.Now create a node by hand and save. Save the generated output, that is going to be your guide.
Now in the place where you want to auto generate the node you can do
Thanks nevets! Will hook_node
Thanks nevets! Will hook_node_insert() trigger regular node creation form validation? Will it trigger Auto Node Title to create automatic node titles?
I'm just a bit vary to do this because I don't want to copy large parts of Organic Groups module into my module, nor do I want to copy large parts of the Date module into my own module. It just seems excessive, especially when I now know that there is possibly a way to do this with fewer lines of code.
(Yes, I do have Devel module installed, so dpm() works fine).
Note you are only
Note you are only implementing hook_node_insert() so you can see the node structure. node_save() is the one used to save the node. It does not do validation but it does trigger other modules behaviors such as auto node title. When I use this approach all the values are controlled so I know they are valid.
Will hook_node_insert() and
Will hook_node_insert() and node_save() trigger hook_form_alter() to allow other third party modules to modify the node directly?
May I ask one straight-forward (hopefully) yes-or-no question which was the topic for me seeking help here; can drupal_execute() submit node forms automatically?
Modules use hook_form_alter()
Modules use hook_form_alter() to alter forms, they re-act to hooks like hook_node_presave(), hook_node_insert() and hook_node_update().
node_save() triggers hook_node_presave() and hook_node_insert() or hook_node_update() (plus some additional hooks).
So if you build the node correctly other modules like OG will do their thing.
I tried node_save() and it
I tried node_save() and it turns out not all modules (Date, OG and one other module using hook_form_alter()) didn't fully populate all fields unfortunately.
I also tried to submit the forum on hook_init(), it triggers the correct form function, and the form contains all values I need saved, but it never actually creates a new node: