如何在网站的正文中插入Drupal表单?

时间:2013-07-11 23:27:04

标签: php drupal drupal-forms

我正在编辑一个页面,我希望页面中有一个drupal表单。

我知道如何制作drupal表单,但是当我编辑' body'阻止并插入一些php,它显示在模板之外

我可以插入一些参数,如

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

还是什么?

提前致谢

$ output = drupal_get_form(contact_form,'节点1');

drupal_render($输出);

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);
};

我添加了整个代码,因为我得到的答案对我没用。

2 个答案:

答案 0 :(得分:1)

你需要使用

drupal_render($输出)

这是一篇很好的文章,解释https://drupal.org/node/224333#unrendered

我刚刚使用以下代码创建了一个模块,您的数组未正确关闭

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);
};

然后使用php内容过滤器

将其添加到节点主体中
<?php 
$output = drupal_get_form('contact_test_form');
return drupal_render($output);
?>

答案 1 :(得分:0)

假设您使用Webform创建此表单...

如果您只想在页面末尾显示表单,请转到表单高级设置,然后单击“available as block”

然后在块部分,将其添加到“主要内容部分”并配置块。
在特定页面上显示块下,写下您希望它出现的页面。


以编程方式打印块

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