Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By applynx on
Hello,
I am new to drupal development. I am trying to insert a user form into a node of a certain type. I am doing so by calling drupal_get_form('mymodule') from within mymodule_node_view, then building the form array, and then inserting the form into $node->content. The form renders correctly, but the mymodule_validate and mymodule_submit functions are never called. What am I missing?
Thanks in advance
Comments
The first thing you are
The first thing you are missing is your code.
Sorry
I thought the flaw might be in my approach. Here is the code ...
The result is that the text field and button appear, but submitting the button does not trigger either validate or submit function.
Try changing
You have a couple of problems. First, you can't add to the form after it has been generated with drupal_get_form. This is actually your problem. When you generate the form, the form element 'myfield' doesn't exist. As such, when the form is submitted, Drupal will not do anything with myfield, as it is a security risk (someone may have added it on the user's side maliciously. You need to add myfield into your form definition, so that it is generated as part of your call to drupal_get_form.
Next, this:
Should be this:
Thank you for your reply
The result of this change is that the form is no longer rendered. The word 'Array' appears where the form had been.
Show your current code.
Show your current code.
Code with form as markup array ...
The result is the word 'Array' is rendered instead of the form.
There is any way to set
There is any way to set custom form position.... Like can we add the form into bottom of page.
My mistake
This worked fine - I was expecting to see my echo statements when submitting the form like I do in the node_view hook, but it looks like they are being ignored because the validate and submit steps occur prior to the page render step. If I add an exit command, I see my echo output, and evidence my functions are being called. Thank you to everybody who assisted me.
If by everybody, you mean me,
If by everybody, you mean me, you're welcome :D
Passing $node
I used the above strategy of trying to modify the form in the node_view() function, but like Jaypan said, it doesn't work. My objective was the pass the node object to the rendered form. I realized that I can pass variables to the form function with the use of drupal_get_form() and that fixed my problem.