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

Jaypan’s picture

The first thing you are missing is your code.

applynx’s picture

I thought the flaw might be in my approach. Here is the code ...

function mymodule_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'my_custom_content_type') {

    $form = drupal_get_form('mymodule_form');
    
      $form['myfield'] = array (
        '#type' => 'textfield',
        '#name' => 'myfield',
        '#title' => t('My Field'),
        '#default_value' => t('Default Field Value'),
      );

    $node->content['mymodule_form'] = $form;

  }

}


function mymodule_form($form, &$form_submit) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Custom Submit Button Text'),
    );
  return $form;
}

function mymodule_form_validate ($form, &$form_state) {
  echo("try to validate");
}

function mymodule_form_submit($form, &$form_state) {
  echo("submitting form");
}

The result is that the text field and button appear, but submitting the button does not trigger either validate or submit function.

Jaypan’s picture

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:

$node->content['mymodule_form'] = $form;

Should be this:

$node->content['mymodule_form'] = array('#markup' => $form);
applynx’s picture

The result of this change is that the form is no longer rendered. The word 'Array' appears where the form had been.

Jaypan’s picture

Show your current code.

applynx’s picture

The result is the word 'Array' is rendered instead of the form.

function mymodule_node_view($node, $view_mode, $langcode) {

    $form = drupal_get_form('mymodule_form');

      $form['myfield'] = array (
        '#type' => 'textfield',
        '#name' => 'myfield',
        '#title' => t('My Field'),
        '#default_value' => t('Default Field Value'),
      );

    $node->content['mymodule_form'] = array('#markup' => $form);

}


function mymodule_form($form, &$form_submit) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Custom Submit Button Text'),
    );
  return $form;
}

function mymodule_form_validate ($form, &$form_state) {
  echo("try to validate");
}

function mymodule_form_submit($form, &$form_state) {
  echo("submitting form");
}
rachanakamlesh’s picture

There is any way to set custom form position.... Like can we add the form into bottom of page.

applynx’s picture

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.

Jaypan’s picture

If by everybody, you mean me, you're welcome :D

AndraeRay’s picture

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.



function example_node_view($node, $view_mode, $langcode) 


    $form = drupal_get_form('example_form', $node);

    $node->content['example_form'] = array('#markup' => drupal_render($form));

}


function example_form($form, &$form_state, $node)
{
	// now I add a text field to the form
	// with a label and fixed dimensions (you never know...)

	$form['first_name'] = array
		(
		  '#title' => t('First Name'),
		  '#type' => 'textfield',
		  '#size' => 32,
		  '#maxlength' => 128,
		);

	$form['user_id'] = array(
	  '#title' => t('Label for the text box'),
	  '#type' => 'hidden',
	  '#value' => $node->uid,
	);

		
	// now I add also a button
	$form['submit'] = array
	(
	   '#type' => 'submit',
	   '#value' => t('Submit'),
	);
	
	$form['#submit'][] = 'example_submit_handler';
	return $form;
}