在drupal中删除多个项目之前请求确认
我在删除单个项目之前请求确认,效果很好,如下所示:在drupal中删除多个项目之前请求确认,drupal,drupal-7,admin,confirm,Drupal,Drupal 7,Admin,Confirm,我在删除单个项目之前请求确认,效果很好,如下所示: function tema_delete_confirm ($form, &$form_state, $tema) { $form['id'] = array('#type' => 'value', '#value' => $tema); return confirm_form ($form, 'Are you sure you want to delete (tema: '.$tema.'
function tema_delete_confirm ($form, &$form_state, $tema) {
$form['id'] = array('#type' => 'value', '#value' => $tema);
return confirm_form ($form,
'Are you sure you want to delete (tema: '.$tema.')?',
'admin/temas',
'This action cannot be undone.',
'Delete',
'Cancel'
);
}
/**
* Executes "tema" deletion.
*
* @see tema_delete_confirm()
*/
function tema_delete_confirm_submit ($form, &$form_state) {
if ($form_state['values']['confirm']) {
$id = $form_state['values']['id'];
tema_delete($id);
drupal_set_message('Tema: '.$id.' has been deleted.');
}
}
问题是对于多个删除项,用checkox标记,确认页面从不提示,提交后我得到hellolog,没有确认页面
/**
* Multiple "temas" deletion confirmation form for temas_admin_content().
*/
function temas_multiple_delete_confirm ($form, &$form_state, $temas) {
$form['temas'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
// array_filter returns only elements with TRUE values
foreach ($temas as $id => $value) {
$title = db_query('SELECT nombre FROM {sescam_tema} WHERE id = :id', array(':id' => $id))->fetchField();
$form['temas'][$id] = array(
'#type' => 'hidden',
'#value' => $id,
'#prefix' => '<li>',
'#suffix' => check_plain($title) . "</li>\n",
);
}
$form['#submit'][] = 'temas_multiple_delete_confirm_submit';
$confirm_question = format_plural(count($temas),
'Are you sure you want to delete this item?',
'Are you sure you want to delete these items?');
return confirm_form($form,
$confirm_question,
'admin/temas',
'This action cannot be undone.',
'Delete',
'Cancel');
}
/**
* Form submission handler for temas_multiple_delete_confirm().
*/
function temas_multiple_delete_confirm_submit ($form, &$form_state) {
die('hola');
if ($form_state['values']['confirm']) {
temas_delete_multiple(array_keys($form_state['input']['temas']));
$count = count($form_state['input']['temas']);
drupal_set_message(format_plural($count, 'Eliminado 1 tema.', 'Eliminados @count temas.'));
}
$form_state['redirect'] = 'admin/temas';
}
知道我做错了什么吗?看起来你试图在服务器上保留一些ID,以便在提交表单时删除某些内容……Drupal实际上有一个值元素类型,可以用来存储这些值,而不必在客户端呈现它们;这样做更有意义 您的代码也可以在性能和可读性方面得到改进,只需进行一些小的调整:
function temas_multiple_delete_confirm ($form, &$form_state, $temas) {
$form['temas'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
$args = array(':ids' => $temas);
$data = db_query('SELECT DISTINCT nombre FROM {sescam_tema} WHERE id IN (:ids)', $args)->fetchCol();
$form['temas'] = array(
'#type' => 'value',
'#value' => $data,
);
$form['#submit'][] = 'temas_multiple_delete_confirm_submit';
$confirm_question = format_plural(count($temas),
'Are you sure you want to delete this item?',
'Are you sure you want to delete these items?');
return confirm_form($form,
$confirm_question,
'admin/temas',
'This action cannot be undone.',
'Delete',
'Cancel');
}
function temas_multiple_delete_confirm_submit ($form, &$form_state) {
$temas = $form_state['values']['temas'];
temas_delete_multiple($temas);
$count = count($temas);
drupal_set_message(format_plural($count, 'Eliminado 1 tema.', 'Eliminados @count temas.'));
$form_state['redirect'] = 'admin/temas';
}