Using an excellent tutorial I found here http://groups.drupal.org/node/14682, I created my first form and first module simultaneously. My form correctly shows on the page on which it was intended to display for the purposes of the tutorial.

However, I have a classified ads site. I created the form so that I could create a small contact form on the node of every single node page in the site. However I can't get it to display.

Here is what I need.
1) show my form on every single page I want to show it on.
2) I need the form to work and be visible even if the user is anonymous.

Here is how I was trying to accomplish this.
1) create a module with the form, which I did successfully
2) I wrote a function called display_form() which simply returned drupal_get_form(contact_poster_form)
3) I put the function on the pages I needed it to display on, however, it will not show up.

Can someone please tell me what I'm doing wrong?
the following is the function I have in my template.php file, and the one I am adding to my templates:

function display_contact_form() {
    return drupal_get_form(contact_poster_form);
}

the following is my code for the contact_poster.module:

function contact_poster_menu() {
	$items = array();
	
	$items['contact_poster'] = array(
		'title' => t('Contact Poster Module'),
		'page callback' => 'contact_poster_page',
		'access arguments' => array('access content'),
		'type' => MENU_SUGGESTED_ITEM,		
	);
	
	return $items;
}

function contact_poster_page() {
	return drupal_get_form('contact_poster_form');	
}

function contact_poster_form($form_state) {
	$form = array();
	
	$form['email'] = array(
		'#type' => 'textfield',
		'#title' => t('Email'),
		'#required' => TRUE,
	);
	
	$form['message'] = array(
		'#type' => 'textarea',
		'#title' => 'Message',
		'#rows' => 4,
		'#required' => TRUE,	
	);
	
	$form['nodetitle'] = array(
		'#type' => 'hidden',
		'#value' => $node->title,
	);
	
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => 'Send',
	);
	
	return $form;
}

function contact_poster_form_validate($form_id, &$form_state) {
	//check if the email field is valid
	if(isset($form_values['values']['email'])) {
		if (!valid_email_address($form_values['values']['email'])) {
		   form_set_error('email', t('The e-mail address you specified is not valid.'));
		}
	}
}

function contact_poster_form_submit($form_id, &$form_state) {
	$author = user_load(array('uid' => $node->uid));
	$to = $author->mail;
	$from = $form_state['values']['email'];
	$body = $form_state['values']['message'];
	$nodetitle = $form_state['values']['nodetitle'];
	
	drupal_set_message(t('Your message has been successfully submitted.'));	
	
	
	$message = array(
  	'to' => $to,
  	'subject' => "Contact from your ad titled {$nodetitle}",
  	'body' => $body,
  	'headers' => array('From' => "{$from}"),
 	);
  
  drupal_send_mail($message);
  		
}

Comments

nevets’s picture

Somewhere you need

print display_contact_form();
stephenrobinson’s picture

Have you tried the urls

http://mysite.com/node/add/contact_poster and http://mysite.com/contact_poster

or create a node, e.g. 999 and clone a template file to page-node-999.tpl.php, and replace :

print $contents;

with

print display_contact_form();

S:)

blue92877’s picture

I was expecting the 'return' to actually output to the screen. However, adding print in front of the function solved it. Sometimes it's the simplest things :/

Thanks!