【问题标题】:How to insert a Drupal form within the body of the website?如何在网站正文中插入 Drupal 表单?
【发布时间】:2013-07-11 23:27:04
【问题描述】:

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

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

有没有我可以插入的参数

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

还是什么?

提前致谢

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

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

【问题讨论】:

  • body block是什么意思,是指节点的body字段吗?
  • 是的,我就是这个意思。

标签: php drupal drupal-forms


【解决方案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);
?>

【讨论】:

  • 我添加了您建议的代码,但在我的页面正文中仍然没有得到任何输出。我还用我的所有代码编辑了我的问题,以便您检查。我在日志中收到错误 [PHP Parse error: syntax error, unexpected T_VARIABLE, expecting ')' in /var/www/html/includes/common.inc(1743) : eval()'d code on line 13, referer :xxxxxxxxxxxx/node/115/edit]
  • 你用的是什么主题?尝试使用 Bartik 等默认主题
  • 不完全。我只是把表单做成了一个模块,然后用 hook_menu 把它当作一个页面使用。
【解决方案2】:

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

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

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


以编程方式打印块

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多