0

User is allowed to submit answers from the question node view page.

The form is redirected to the same question node view page after submission. But the form displayed still displays the older values. How can I reset these values?

function answers_custom_node_view($node, $view_mode, $langcode) {

  if (arg(0) == 'node' && is_numeric(arg(1)) && $node->type == 'question') {
    global $user;
        if(node_access("create", "answer", $user)) {
        $node1 = (object) array(
            'uid' => $user->uid,
            'name' => (isset($user->name) ? $user->name : ''),
            'type' => "answer",
            'language' => LANGUAGE_NONE,
        );
        $form_state['build_info']['args'] = array($node1);
        form_load_include($form_state, 'inc', 'node', 'node.pages');
        $form = drupal_build_form('answer_node_form', $form_state);
        $output = drupal_render($form);
        $node->content['answer_form'] = array(
            "#type" => "markup",
            "#markup" => $output,
        );
      }
   }
}

function answers_custom_form_alter(&$form, &$form_state, $form_id) {
   if($form_id == "answer_node_form") {
       if (arg(0) == 'node' && is_numeric(arg(1))) {
        $nid = arg(1);
        $node = node_load($nid);
        if($node->type == "question") {
             $form['#submit'][] = "answers_custom_destination";
        }
       }
   }
}

function answers_custom_destination($form, &$form_state){
   if (arg(0) == 'node' && is_numeric(arg(1))) {
    $nid = arg(1);
    $node = node_load($nid);
    if($node->type == "question") {
        $form_state['rebuild'] = TRUE;
        $form_state['redirect'] = drupal_get_path_alias('node/'. $node->nid);
     }
   }
}

1 Answer 1

4

That happens because you are using $form_state['rebuild'] = TRUE; which is telling Drupal to rebuild the same form, which also mean the form will be repopulated with the last values the users entered. When you then set $form_state['rebuild'] to TRUE, $form_state['redirect'] is ignored, since the user is already redirected to the page containing the form.

The code I would use is the following one.

function answers_custom_form_answer_node_form_alter(&$form, &$form_state) {
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    // See https://api.drupal.org/api/drupal/includes%21menu.inc/function/menu_get_object/7.
    $node = menu_get_object();

    if ($node->type == "question") {
      $form['#submit'][] = "answers_custom_destination";
    }
  }
}

function answers_custom_destination($form, &$form_state) {
  // Since the form submission handler is added only when the user is seeing node/%node,
  // there is no need to verify that again.
  $form_state['redirect'] = FALSE;
}

As side note, since the form alter hooks and the form submission handlers share $form_state, which is passed by reference, if something needs to be shared between those functions, it can be added to $form_state. As long as the added array index doesn't conflict with an array index used by Drupal or third-party modules, all is fine.

For example, this code would use $form_state to share a value between the hook altering the form, and the form submission handler.

function answers_custom_form_answer_node_form_alter(&$form, &$form_state) {
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    // See https://api.drupal.org/api/drupal/includes%21menu.inc/function/menu_get_object/7.
    $node = menu_get_object();

    if ($node->type == "question") {
      $form_state['answers_custom_question'] = $node;
      $form['#submit'][] = "answers_custom_destination";
    }
  }
}

function answers_custom_destination($form, &$form_state) {
  // Since the form submission handler is added only when the user is seeing node/%node,
  // there is no need to verify that again.
  $form_state['redirect'] = drupal_get_path_alias('node/' . $form_state['answers_custom_question']->nid);
}

Reference

7
  • After the form is submitted, the page is redirected to node page view, but it should redirected to the previous page, where the answer was submitted.
    – pritywiz
    Commented Sep 9, 2013 at 9:19
  • Aren't you adding the form to write an answer in the node page showing the question? If that is the case, the code I wrote should redirect to the page containing the question. If by previous page you mean any page viewed before seeing the question, Drupal cannot redirect there, if you don't know the path of that page.
    – avpaderno
    Commented Sep 9, 2013 at 9:43
  • Yes, I mean by the node page showing the question. with correction to the line $form['#submit'][] = "answers_custom_destination"; to $form["actions"]["submit"]['#submit'][] = "answers_custom_destination";, the code is working, but the form still displays the previous entered values.
    – pritywiz
    Commented Sep 9, 2013 at 9:48
  • I have retained the previous line $form_state['redirect'] = FALSE;. Now the page is redirected to the question page, the answer node form is still displaying the old values. I tried, $form_state['values'] = array(); and $form_state['input'] = array(); but does not clear the form. I am testing in Firefox could this be the reason.
    – pritywiz
    Commented Sep 9, 2013 at 10:12
  • I will update my answer after I test the code on my test site.
    – avpaderno
    Commented Sep 9, 2013 at 14:10

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.