当我按某些内容上的删除按钮时,我会进入确认页面。删除选项是一个按钮,而取消选项是一个链接。这看起来很奇怪。我发现在drupal中有一个form_confirm()函数,但我无法理解如何使用它。有谁知道如何将取消链接变成按钮?
答案 0 :(得分:2)
取消链接看起来像链接的原因是因为它是链接<a>
,而确认按钮是表单提交元素<input type="submut>
。
如果您想要取消链接,看起来像提交按钮,您可以使用纯CSS执行此操作。
答案 1 :(得分: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;';
}
}
答案 2 :(得分: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;
}
}
答案 3 :(得分: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。
祝你好运, BASO。