0

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

我知道如何制作drupal表单,但是当我编辑“body”块并插入一些php时,它会显示在模板之外

是否有一些我可以插入的参数

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

或者其他的东西?

提前致谢

$output = drupal_get_form(contact_form, 'node 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);
};

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

4

2 回答 2

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);
?>
于 2013-07-12T08:20:50.840 回答
0

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

如果您只想在页面末尾显示表单,请转到表单高级设置并单击“作为块可用”

然后在块部分,将其添加到“主要内容部分”并配置块。
Show block on specific pages下,写下您希望它出现的页面。


以编程方式打印块

$block = module_invoke('webform', 'block_view', 'client-block-1'); //add your block id
print render($block['content']); 
于 2013-07-12T08:19:30.150 回答