How to embed the EMAIL CONTACT FORM

Zoologico - January 13, 2008 - 15:18
Project:Email Field
Version:5.x-1.x-dev
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:needs review
Description

Hi folks,


My ultimate question is how to use the email module to embed the email contact form, but I am also curious as to why the form isn't being rendered in my node when I use this code:

<?php print drupal_get_form('email_mail_page_form'); ?>

I am trying to get the email contact form generated by this module into the node itself. I looked through the module and I narrowed it down to two functions that coudl help me get this accomplished:

function email_mail_page_form()

and/or

function email_mail_page($nid=null, $fieldname=null)

I've used:

<?php print drupal_get_form('email_mail_page_form'); ?>

This does not generate anything on the page. :( I expected this to at least render the form created by this code in the email.module file:

function email_mail_page_form() {
  global $user;
 
  if ($user->uid) {
    $edit['name'] = $user->name;
    $edit['mail'] = $user->mail;
  }

  $form['#token'] = $user->name . $user->mail;
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Your name'),
    '#maxlength' => 255,
    '#default_value' => $edit['name'],
    '#required' => TRUE,
  );
  $form['mail'] = array('#type' => 'textfield',
    '#title' => t('Your e-mail address'),
    '#maxlength' => 255,
    '#default_value' => $edit['mail'],
    '#required' => TRUE,
  );
  $form['subject'] = array('#type' => 'textfield',
    '#title' => t('Subject'),
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['message'] = array('#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  $form['submit'] = array('#type' => 'submit',
    '#value' => t('Send e-mail'),
  );
  return $form;
}

I then tried:

<?php print drupal_get_form('email_mail_page', $node->nid, $node->field_business_email); ?>

This returns a Drupal 404 error I suspect due to this code in the email.module file:

  if (empty($nid) || empty($fieldname)) {
    drupal_not_found();
    return;
  }
  $node = node_load(intval($nid));
  if (!$node) {
    drupal_not_found();
    return;
  }
  // Validate field name
  $types = content_types($node->type);
  if (!isset($types['fields'][$fieldname]) ||
      $types['fields'][$fieldname]['type'] != 'email' ||
      $types['fields'][$fieldname]['widget']['link_type'] != 'form') {
    drupal_not_found();
    return;
  }
  $field = $node->$fieldname;
  if (empty($field) || empty($field[0]['email'])) {
    drupal_not_found();
    return;
  }


My ultimate question is how to use the email module to embed the email contact form, but I am also curious as to why the form isn't being rendered in my node when I use this code:

<?php print drupal_get_form('email_mail_page_form'); ?>

Thanks kindly for reading.

#1

worpx - February 27, 2008 - 02:17

Hi there,

When I invoke

<?php
print drupal_get_form('email_mail_page_form');
?>
it works fine for me; it display the default mail form for the site; but in order to get it from your node
<?php
print drupal_get_form('email_mail_page', $node->nid, $node->field_business_email);
?>
should work (provided that field_business_email is a CONTEMPLATE).

Cheers

#2

auctionduke - May 26, 2008 - 21:29

Where exactly do you embed this code (which file?):

<?php
print drupal_get_form('email_mail_page_form');
?>

Thanks

#3

aguskii - May 28, 2008 - 10:24

Hi all,
Very interesting question!!
I´m new in drupal and I would like also to know how to embed the email form, and some extra thing.. how to fill up the subject field with the title of the node where it is embed? (sorry about my english).
I´m using drupal 6.2 but I think it should be pretty similar to 5.x.
Thanks in advance.

#4

tanyi - September 1, 2008 - 05:55

Were you able to find the answer to your question i.e. how to fill up the subject field with the title of the node? I am trying to do the same thing but unable.

#5

mboudey - September 5, 2008 - 00:22

+1

#6

tanyi - September 16, 2008 - 05:42

Any help on the above request please?

#7

GiorgosK - November 16, 2008 - 16:38

Try this approach http://drupal.org/node/335020
its for drupal 6 but should not take much to change for drupal 5

#8

milos.kroulik - March 22, 2009 - 16:34

[#1] doesn't work for me. I always got "Page not found". I tried through block and contemplate. I don't think there is any difference.

#9

mjoyce71 - March 23, 2009 - 15:12

I couldn't figure it out and just ended up using a web form to email service:

http://www.formmailhosting.com

Good part is they send you the email of the form results (of course) but they also store the submissions in MS Excel which is a nice feature too.

- Mark

#10

milos.kroulik - March 23, 2009 - 21:14
Version:5.x-1.x-dev» 6.x-1.x-dev

I sort of solved it. But through such ugly hack (it uses Iframe and hacks Email Field module directly), that I probably shouldn't recommend it to anyone. So I will describe it only briefly. Reply if you're interested.

  • I created another "formatter" for module, that outputs Contact page corresponding field in Iframe (lot of ugly hacking)
  • I used "theme suggestion" to override page template for pages, whose alias begin with "email". I followed this comment. Template outputs only form, that I need.
  • That's basically all. You can see my result at: http://skipdemo.milhaus.cz/organizace/mestska-knihovna-litomysl
  • You may need to make also plain e-mail address accessible. I used computed field to do it.

Of course I am still interested in better solutions.

#11

GiorgosK - March 24, 2009 - 17:30
Version:6.x-1.x-dev» 5.x-1.x-dev

The majority of code on this support request still applies to Drupal 5.x
thus changing the version to 5.x otherwise it would be confusing to the people that see this issue in the future

#12

NaX - October 19, 2009 - 09:22
Status:active» needs review

This one threw me for a bit, but this is how I solved the problem without any hacking, please note this is for D6.

Because email_mail_page() is not a form function a but normal menu call back. In this function a drupal_get_form gets called on email_mail_page_form()
But email_mail_page() does a bunch of stuff including checking for flood thresholds.

So I could call drupal_get_form on email_mail_page_form directly but instead I used the code from email_mail_page() and modified it to keep the extra checking.

Here is the steps I took.

I created content type called contact and added an email field called field_contact_email.

I then created a template file for my new content type called node-contact.tpl.php and saved it in my theme.
I copied the contents of node.tpl.php into my node-contact.tpl.php.

At the end of my new template file I added the following code.

<?php
 
if ($page) {
   
// fallback if email module is every disabled
   
if (module_exists('email')) {
     
// Setup required variables
     
$field_name = 'field_contact_email';
     
$show_form  = TRUE;
     
     
// Taken from email_mail_page(), but modified.
     
if (module_exists('content_permissions')) {
        if (!
user_access('view '. $field_name)) {
         
$show_form = FALSE;
        }
      }
     
     
$field = $node->$field_name;
     
$email = $field[0]['email'];
     
$types = content_types($node->type);
     
// Validate field name
     
$types = content_types($node->type);   
      if (empty(
$email) ||
          !isset(
$types['fields'][$field_name]) ||
         
$types['fields'][$field_name]['type'] != 'email' ||
          (
$types['fields'][$field_name]['display_settings']['teaser']['format'] != 'contact' &&
         
$types['fields'][$field_name]['display_settings']['full']['format'] != 'contact')) {
       
$show_form = FALSE;
      }   

      if (
$show_form) {
        if (!
flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
          print
t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('email_hourly_threshold', 3)));
        }
        else {
          print
drupal_get_form('email_mail_page_form', $node, $field_name, $email);       
        }
      }
    }
   
   
// Change the page title
   
drupal_set_title(t('Contact: !title', array('!title' => $node->title)));
  }
?>

Hope this helps other people.

#13

Summit - November 6, 2009 - 09:27

Hi,

On which function you added this php-code in template.php please? I have the acquia_slate theme and am not seeing were to add this.
Looking for same sort solution on D6. Can you also add fields to the contactform?
Thanks for your reply in advance!

Greetings, Martijn

#14

NaX - November 6, 2009 - 14:11

Sorry I was not clear, you add it to your new tpl.php file. Just remember that I called everything contact and you will need to change that in the code and file names.

EG:

<?php
$field_name
= 'field_contact_email';
?>

#15

Summit - November 6, 2009 - 14:26

Hi, will look into it more!
Do you also know how to add a field to the form, say the url of the contact?
greetings,
Martijn

#16

pauldawg - November 11, 2009 - 03:34

Interesting. I've been looking at doing this as well, and I will probably try out the approach in #12. In looking at the code, although it's not a "hack" per se, it does look like it is copying some basic code from the email contact form module, is that true? If so, might there be some issues maintaining this to stay in sync with updates to that module?

 
 

Drupal is a registered trademark of Dries Buytaert.