2010-08-15 46 views
1

當我按下某些內容上的刪除按鈕時,我被帶到確認頁面。刪除選項是一個按鈕,而取消選項是一個鏈接。這看起來很奇怪。我發現在drupal中有一個form_confirm()函數,但我無法理解如何使用它。有誰知道如何使取消鏈接到一個按鈕?Drupal - 使確認頁面上的取消鏈接一個按鈕

回答

0

或使用本不需要JavaScript(和更換用的preg_match()eregi()...

if ($form['#theme'] == 'confirm_form') { 
    $no = $form['actions']['cancel']['#value']; 
    if (!is_null($no)) { 
     // Get the text to put on the cancel button 
     $value = preg_replace('/(<\/?)(\w+)([^>]*>)/e', '', $no); 
     preg_match('/href\s*=\s*\"([^\"]+)\"/', $no, $href); 
     $form['actions']['cancel']['#value'] = ''; 
     $form['href']=array(
     '#type'=>'value', 
     '#value'=>$href[1], 
    ); 

     // Add our own button 
     $form['actions']['docancel'] = array(
     '#type' => 'submit', 
     '#name' => 'cancel', 
     '#submit' => array('mymodule_confirm_form_cancel'), 
     '#value' => $value, 
    ); 

    } 
    } 

function mymodule_confirm_form_cancel(&$form,&$form_state) { 
    $href=$form['href']['#value']; 
    if (!is_null($href)) { 
    $form['#redirect']=$href; 
    } 
} 
2

原因取消鏈接看起來像一個鏈接,因爲它是一個鏈接<a>,而確認按鈕,是一個表單提交元素<input type="submut>

如果你想取消鏈接,看起來像一個提交按鈕,你可以用純CSS做到這一點。

+0

在Safari(和Chrome?)中,使用'-webkit-appearance:push-button'就很簡單。 – 2010-08-15 20:45:06

+0

我一直在環顧四周,但無法找到正確的CSS代碼。我發現的只是如何製作時尚的按鈕,但我只想要一個普通的系統按鈕。 – Toxid 2010-08-16 21:41:37

1

使用hook_form_alter(),試試這個:

if($form['#theme'] == 'confirm_form') { 
    $no = $form['actions']['cancel']['#value']; 
    if (!is_null($no)) { 
     // Get the text to put on the cancel button 
     $value = preg_replace('/(<\/?)(\w+)([^>]*>)/e', '', $no); 
     eregi('m|href\s*=\s*\"([^\"]+)\"|ig', $no, $href); 

     $form['actions']['cancel']['#value'] = ''; 

     // Add our own button 
     $form['actions']['docancel'] = array(
     '#type' => 'button', 
     '#button_type' => 'reset', 
     '#name' => 'cancel', 
     '#submit' => 'false', 
     '#value' => $value, 
     '#attributes' => array(
      'onclick' => '$(this).parents("form").attr("allowSubmission", "false");window.location = "'.$href[1].'";', 
     ), 
    ); 
     // Prevent the form submission via our button 
     $form['#attributes']['onsubmit'] = 'if ($(this).attr("allowSubmission") == "false") return false;'; 
    } 
    } 
0

對於Drupal 7我使用:

/** 
* Implements hook_form_alter(). 
*/ 
function yourmodule_form_alter(&$form, $form_state, $form_id) { 
    // Change 'cancel' link to 'cancel' button. 
    if ($form['#theme'] == 'confirm_form') { 
    if ($form['actions']['cancel']['#type'] == 'link') { 
     $title = $form['actions']['cancel']['#title']; 
     $href = $form['actions']['cancel']['#href']; 
     if (!is_null($title) and !is_null($href)) { 
     // Disable Cancel link. 
     $form['actions']['cancel']['#title'] = ''; 
     // Add our own Cancel button. 
     $form['actions']['docancel'] = array(
      '#type' => 'submit', 
      '#name' => 'cancel', 
      '#submit' => array('yourmodule_confirm_form_cancel'), 
      '#value' => $title, 
     ); 
     } 
    } 
    } 
} 

/** 
* Redirect to previous page after confirm form cancel(). 
*/ 
function yourmodule_confirm_form_cancel(&$form, &$form_state) { 
    $href = $form['actions']['cancel']['#href']; 
    if (!is_null($href)) { 
    $form_state['redirect'] = $href; 
    } 
} 

Drupal 8的問題也有報道,但Drupal核心團隊無意解決核心問題。請參閱Drupal支持請求Change confirmation form Cancel link to a button

最好的問候, 巴索。