1

当我在某些内容上按下删除按钮时,我会进入确认页面。删除选项是一个按钮,而取消选项是一个链接。这看起来很奇怪。我发现drupal中有一个form_confirm()函数,但我不明白如何使用它。有谁知道如何将取消链接变成一个按钮?

4

4 回答 4

2

取消链接看起来像链接的原因是因为它是一个链接<a>,而确认按钮是一个表单提交元素<input type="submut>

如果你想让取消链接看起来像一个提交按钮,你可以用纯 CSS 来实现。

于 2010-08-15T14:11:59.920 回答
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;';
    }
  }
于 2010-08-19T16:24:32.023 回答
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;
  }
}
于 2010-08-29T21:24:57.753 回答
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 支持请求更改确认表单 取消按钮链接

最好的问候,巴索。

于 2014-09-15T14:14:02.763 回答