Seeing that my modules are irretrievably borked, I want to generate a form from a php document in the nodes.

Only problem (thus far!) is that my form does not render.

My script is as follows:

global $user;
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['title'] = array(
  '#type' => 'textfield', 
  '#title' => t('Article Name'),
  '#size' => 60,
  '#maxlength' => 64, 
  '#required' => TRUE
);

$form['body'] = array(
  '#type' => 'textarea', 
  '#title' => t('Description'), 
  '#default_value' => $node->body
);

$form['document_upload'] = array(
  '#type' => 'file', 
  '#title' => t('Upload The Article'), 
  '#size' => 40,
);

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Save'),
);
$form['#validate'] = array('kwbse_node_form_validate');

function kwbse_node_form_validate($form, &$form_state) {
  if (isset($form['document_upload'])) {
    $validators = array(
      'file_validate_extensions' => array('pdf')
    );
    $file = file_save_upload('document_upload', $validators);
    if (!$file)
      form_set_error('upload', 'You must select a valid file to upload.');
    else {
      $form_state['values']['taxonomy[2]'] = 1;
			$node = node_submit($form_state['values']);
			$node->type = 'knoledgebase';
			$node->uid = $user->uid;
			node_save($node);
    }
  }
}

Clearly I need to call a function to render the form on the page...

Comments

stevenc’s picture

You need to wrap the $form object code into a function that returns the object. Then, you call call to render the form.

Reference:
http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6

---------------------------------
Steven Wright

Slalom

davecoventry’s picture

Ah! Thanks Steven,

The clue was the final comment on the link you provided.

If the form is defined in a function called spaceman_spiff_form(form_state), then the function would appear to be called by the command echo drupal_get_form('spaceman_spiff_form');

Not the way most languages work, but nevermind!

I'll test it out and report back.

Dave

davecoventry’s picture

function spaceman_spiff_form($form_state){
	$form=Array();
	$form['#attributes'] = array('enctype' => "multipart/form-data");
	$form['title'] = array(
		'#type' => 'textfield', 
		'#title' => t('Article Name'),
		'#size' => 60,
		'#maxlength' => 64, 
		'#required' => TRUE
	);
	//more form elements....
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Save'),
	);
	$form['#validate'] = array('kwbse_node_form_validate');
	return $form;
	
}

function kwbse_node_form_validate($form, &$form_state) {
	global $user;
  if (isset($form['document_upload'])) {
    $validators = array(
      'file_validate_extensions' => array('pdf')
    );
    $file = file_save_upload('document_upload', $validators);
    if (!$file)
      form_set_error('upload', 'You must select a valid file to upload.');
    else {
      $form_state['values']['taxonomy[2]'] = 1;
			$node = node_submit($form_state['values']);
			$node->type = 'knoledgebase';
			$node->uid = $user->uid;
			node_save($node);
    }
  }
}

echo drupal_get_form('spaceman_spiff_form');//<----- The important line right here

Works fine.