在删除drupal中的多个项目之前请求确认

时间:2014-10-07 14:25:35

标签: 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.')?',
        '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,永远不会提示确认页面,提交后我得到hello日志而没有确认"页面"

/**
 * 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';
}

知道我做错了什么吗?

1 个答案:

答案 0 :(得分:-1)

看起来你正试图在服务器上保留一些ID,以便在提交表单时删除某些内容...... Drupal实际上有一个value元素类型可用于存储值而不必将它们呈现给客户端;使用它会更有意义。

您的代码也可以通过一些小的调整来提高性能和可读性:

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';
}