簡體   English   中英

如何在網站正文中插入Drupal表單?

[英]How to insert a Drupal form within the body of the website?

我正在編輯頁面,並且希望頁面中有一個drupal表單。

我知道如何制作Drupal表單,但是每當我編輯“ body”塊並插入一些php時,它就會顯示在模板外部

有一些我可以插入的參數嗎

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

或者其他的東西?

提前致謝

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

我添加了整個代碼,因為我得到的答案對我不起作用。

你需要使用

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

假設您使用Webform創建了此表單...

如果您只想在頁面末尾顯示表單,請轉到表單高級設置,然后單擊“以塊形式提供”

然后,在塊部分中,將其添加到“主要內容部分”,並配置該塊。
“在特定頁面上顯示塊”下,編寫您希望其顯示的頁面。


以編程方式打印塊

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM