webadmin's picture

Drupal 6 How to Creating a form in a block

Drupal

I think that you are asking for a form in a block where users can easily add a question without going to the usual node/add page. After some messing around, I've got this working on my site. I first created a custom content type called question and added a function to my template.php file. Last, I created a block with a php input filter and called the function from my template.php file.

Please know that there is probably much better way to do this. To create the functions below, I worked from this blog posting.

Here's the code I added to my template.php file:

<?php
function custom_question() {
return drupal_get_form('custom_question_form');
}
function custom_question_form() {
$form['title'] = array(
'#type' => 'textfield',
/*If you want to change the name of the Question field do it below between the single quotes*/
'#title' => 'Question',
'#size' => 80,
);

$form['body'] = array(
'#type' => 'textarea',
/*If you want to change the name of the answer field do it below between the quotes*/
'#title' => 'Answer',
);

$form['submit'] = array(
'#type' => 'submit',
/*If you want to change the name of the button do so below between the quotes*/
'#value' => 'Post',
);
return $form;
}

function custom_question_form_submit($form_id, $form) {
global $user;
$form_id = 'question_node_form';
$node = array(
'uid' => $user->uid,
'name' => $user->name,
'type' => 'question',
);
$form_values = array(
'title' => $form['title'],
'name' => $user->name,
'body' => $form['body'],
);
watchdog('debug', 'Saving question');
drupal_execute($form_id, $form_values, $node);
}

?>

then in add new block :

<?php print custom_question() ?>