Drupal7 form表單二次開發要點與執行個體_PHP教程

來源:互聯網
上載者:User
請記得收藏此文,在你進行Drupal 7 custom module時,經常會用到的form 表單的跳轉或重載。

主要匯總三個要點:

1.頁面提交後,經過#submit處理後,需要redirect 跳轉到另外一個頁面。
2.url路徑中存在destination參數時,頁面直接跳轉到destination所指的url,無法控制的問題。
3.form表單如何?multiple steps forms 多個步驟,或者表單提交後,如何在表單擷取到提交上來的值。

一、Form 表單 redirect(跳轉)到另外一個頁面

$form_state['redirect'] 的值可以是字串或者數組,值通過url後,產生跳轉地址。

複製代碼 代碼如下:$form_state['redirect'] = array(
'node/123',
array(
'query' => array(
'foo' => 'bar',
),
'fragment' => 'baz',
}
//頁面將會跳轉到 node/123?foo=bar#baz

複製代碼 代碼如下:$form_state['redirect'] = 'node/123'
//頁面將會跳轉到 node/123


如果不指定$form_state['redirect'] 的值,預設跳轉到當前頁面。drupal_goto(current_path(), array(‘query' => drupal_get_query_parameters())); API中是這樣執行的。

二、Form 表單 destination(目的地)被指定時也可以改變跳轉的地址

在drupal_goto 函數中,你可以看到如果url路徑中存在destination參數,頁面直接就到destination所指向的連結,導致某些表單下的多個按鈕提交後,本應redirect 跳轉的頁面也不盡不同。

於是在form的#submit 函數中,某些操作時可以直接刪除掉destination。

複製代碼 代碼如下:if (isset($_GET['destination'])) {
$form_state['redirect'] = array('next_step_page_url', array('query' => drupal_get_destination()));
unset($_GET['destination']);
}
我採取的方法是,重新定義一個url並繼續傳遞destination,但是將$_GET中的destination刪除掉。但是一般還是會經常用到destination這個目的地的跳轉。

三、Form 表單 實現multiple steps多個步驟,Form表單重載,擷取Form提交的值

這些問題其實歸根到底都是一個意思,就是讓表單繼續提交下去。而不是重新整理頁面。只需在form 表單的 #submit 函數中 執行以下代碼:

複製代碼 代碼如下:if ($form_state['values']['op'] == t("Next Step")) {
$form_state['rebuild'] = TRUE;
$form_state['storage']['users'] = $form_state['values']['users'];
}
在form的define定義中即可擷取到$form_state['storage']['users']這個值。

參考Drupal7 相關API函數:

drupal_redirect_form
drupal_goto
drupal_get_destination

http://www.bkjia.com/PHPjc/736823.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/736823.htmlTechArticle請記得收藏此文,在你進行Drupal 7 custom module時,經常會用到的form 表單的跳轉或重載。 主要匯總三個要點: 1.頁面提交後,經過#submit處理...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.