Drupal - اجعل رابط إلغاء على زر التأكيد A
سؤال
عندما أضغط على زر الحذف في بعض المحتوى ، نُقل إلى صفحة تأكيد. خيار الحذف هو زر ، في حين أن خيار إلغاء هو رابط. هذا يبدو غريبا جدا. لقد وجدت أن هناك وظيفة form_confirm () في دروبال ، لكن لا يمكنني فهم كيفية استخدامها. هل يعرف أي شخص كيفية جعل رابط إلغاء في زر؟
المحلول
أو هذا باستخدام أي javaScript (واستبدال eregi () مع preg_match () ...
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;
}
}
نصائح أخرى
السبب في أن رابط الإلغاء يبدو وكأنه رابط ، لأنه رابط <a>
, ، في حين أن زر التأكيد ، هو عنصر إرسال نموذج <input type="submut>
.
إذا كنت ترغب في عمل رابط إلغاء ، لتبدو وكأنها زر إرسال ، يمكنك القيام بذلك باستخدام CSS Pure.
باستخدام 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;';
}
}
لدروبال 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 Core ليس لديه أي نية لحل المشكلة في Core. انظر طلب دعم دروبال تغيير نموذج التأكيد إلغاء الرابط إلى زر.
مع أطيب التحيات ، باسو.