0
votes

I'm editing a page and I want there to be a drupal form in the page.

I know how to make drupal forms, but whenver I edit the 'body' block and insert some php, it displays outside of the template

Is there some parameter I can insert like

 $output = drupal_get_form(my_form, 'node 1') 

or something?

Thanks in advance

$output = drupal_get_form(contact_form, 'node 1');

drupal_render($output);

function contact_form($form_state) {
    $form['firstname'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE
    $form['lastname'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE
    $form['email_from'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE
    $form['telephone'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#description' => t("Optional"),
        '#size' => 30,
        '#required' => TRUE
    $form['comments'] = array(
        '#type' => 'textarea', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form; 
};

function contact_form_validate($form, $form_state) {
    $error_message = "";
    $string_exp = "/^[A-Za-z .'-]+$/";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }
    if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }
    if(strlen($comments) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }
    if(strlen($error_message) > 0) {
        died($error_message);
    }
};

function contact_form_submit($form, $form_state) {
    // build the body of the email  
    $body = "First Name: ".clean_string($first_name)."<br />"."Last Name: ".clean_string($last_name)."<br />"."Email: ".clean_string($email_from)."<br />"."Telephone: ".clean_string($telephone)."<br />"."Comments: ".clean_string($comments);

    //send
    $message = array(
        'to' => 'xxxxxxxxxxxxxxxxxx',
        'subject' => $email_subject,
        'body' => $body,
        'headers' => array(
            'From' => $email_from,
            'To' => 'xxxxxxxxxxxxxxxxx',
            'Subject' => $email_subject,
            );
drupal_mail_send($message);
};

I added my whole code because the answers I've gotten didnt work for me.

2
What do you mean by body block, do you mean the body field of a node?Daniel Harper
Yes that's what I meant.N Schleder

2 Answers

1
votes

you need to use

drupal_render($output)

This is a good post that at explains https://drupal.org/node/224333#unrendered

I've just created a module with the following code, your arrays weren't closed properly

function contact_test_form($form) {    
    $form['firstname'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE,
        );
    $form['lastname'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE,);
    $form['email_from'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE,);
    $form['telephone'] = array(
        '#type' => 'textfield', 
        '#title' => t('Title of Notice'), 
        '#description' => t("Optional"),
        '#size' => 30,
        '#required' => TRUE,);
    $form['comments'] = array(
        '#type' => 'textarea', 
        '#title' => t('Title of Notice'), 
        '#size' => 30,
        '#required' => TRUE,);
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form; 
};

function contact_form_validate($form, $form_state) {
    $error_message = "";
    $string_exp = "/^[A-Za-z .'-]+$/";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }
    if(!preg_match($string_exp,$first_name)) {
        $error_message .= 'The First Name you entered does not appear to be valid.<br />';
    }
    if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
    }
    if(strlen($comments) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
    }
    if(strlen($error_message) > 0) {
        died($error_message);
    }
};

function contact_form_submit($form, $form_state) {
    // build the body of the email  
    $body = "First Name: ".clean_string($first_name)."<br />"."Last Name: ".clean_string($last_name)."<br />"."Email: ".clean_string($email_from)."<br />"."Telephone: ".clean_string($telephone)."<br />"."Comments: ".clean_string($comments);

    //send
    $message = array(
        'to' => 'xxxxxxxxxxxxxxxxxx',
        'subject' => $email_subject,
        'body' => $body,
        'headers' => array(
            'From' => $email_from,
            'To' => 'xxxxxxxxxxxxxxxxx',
            'Subject' => $email_subject,
            ),);
drupal_mail_send($message);
};

Then add this into a node body using php content filter

<?php 
$output = drupal_get_form('contact_test_form');
return drupal_render($output);
?>
0
votes

Assuming you created this form with Webform...

If you just want to display the form at the end of the page, go to the form advance settings and click on "available as block"

Then on the block sections, add it to the "main content section" and configute the block.
Under Show block on specific pages, write the pages you want it to appear.


To print a block programatically

$block = module_invoke('webform', 'block_view', 'client-block-1'); //add your block id
print render($block['content']);