1

I have created a form to take a node ID and then you select either edit or delete and then I would like it to go to 'node/[nid]/edit' or 'node/[nid]/delete' depending on the selection of the radio button.
Here is my code:

function edit_node_block_info(){
    $blocks['edit_node'] = array(
        'info' => t('Custom Block to allow edit/delete/copy'),
        'status' => 1,
        'region' => 'Primary',
        'weight' => 0,
        'visibility' => BLOCK_VISIBILITY_LISTED,
    );

    return $blocks;
}
/*
 * Implements hook_block_view: sets how the block is displayed
 */
function edit_node_block_view($delta = ''){
    switch ($delta) {
        case 'edit_node' :
            $block['subject'] = t('Edit Nodes');
            $block['content'] = drupal_get_form('edit_node_form');
            break;
    }
    return $block;
}
/*
 * implements custom form/creating custom form for drupal_get_form named above.
 */

function edit_node_form($form, &$form_state) {
    $form['node-id'] = array(
        '#type' => 'textfield',
        '#title' => t('Enter the Node ID'),
        '#max-length' => 8,
        '#required' => 1,
    );
    $form['edit-node'] = array(
        '#type' => 'radios',
        '#title' => t('Select Action'),
        '#options' => array(t('edit'), t('delete')),
        '#required' => 1,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form;
}

function edit_node_form_validate($form, &$form_state){
    $query = db_query('SELECT nid FROM {node}');
    foreach ($query as $result) {
        if (!($form_state['values']['node-id'] != $result)) {
            form_set_error('node-id', 'Node-id must match a current nid.');
        }
    }
}

function edit_node_submit($form, &$form_state){
    if (form_state['values']['edit-node'] == 'edit'){
        $form_state['redirect'] = 'node/' . (form_state['values']['node-id']) . '/edit';
    }
    if (form_state['values']['edit-node'] == 'delete'){
        $form_state['redirect'] = 'node/' . (form_state['values']['node-id']) . '/delete';
    }
}

In the $form array do I need some sort of redirect handler to make it work? Or a print_r or render?

I also tried $link = (form_state['values']['node-id']);

1 Answer 1

0

Looks like the issue I was having was twofold. First I need to add some settings:

$form['node-action'] = array(
        '#type' => 'radios',
        '#title' => t('Select Action'),
        '#options' => array (
            edit => t('edit'),
            delete => t('delete'),
            add => t('add'),
            ),
        '#required' => 1,
    );

Then added:

$form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#submit' => array('edit_node_submit'),
    );

And lastly my submit function:

function edit_node_submit($form, &$form_state){
    $node_id = $form_state['values']['node-id'];
    $node_action = ($form_state['values']['node-action']);
    $form_state['redirect'] = TRUE;
    if ($node_action == 'edit') {
        $form_state['redirect'] = ('node/' . $node_id . '/edit');
        }
    if ($node_action == 'delete'){
        $form_state['redirect'] = 'node/' . $node_id . '/delete';
    }
    if ($node_action == 'add'){
        $form_state['redirect'] = 'node/add/hospitalist-schedule';
    }
}

Everything is working for the most part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.