0

I have a block with a select box and submit button. The main purpose of the select box is to store a variable in the session. I used template_preprocess_node to filter contents based on user selection.

function blackout_preprocess_node(&$vars) {
  $node = $vars['node'];

  // define a variable to hold your rendered content
  $newContent = "";

  if($_SESSION['myid'] == '1' && $node->field_field1[0]['value'] != '') {
    $newContent = $node->field_field1[0]['view'];
  } else if($_SESSION['myid'] == '2' && $node->field_field2[0]['value'] != '') {
    $newContent = $node->field_field2[0]['view'];
  } else {
    $newContent = $node->body;
  }
  if (strlen($newContent)) $vars['content'] = $newContent;
}

Everything is working fine except I have to click submit twice to reload the page contents or I have to move to another page to see re-loaded content. How can I reload content with just one click?

0

1 Answer 1

1

Blocks are rendered in hook_preprocess_page. At that time, all implementations of hook_preprocess_node have already been executed, so you cannot influence how the node is displayed (apart from messing with the HTML in $vars['content']).

If you use Drupal's Form API to implement the select list and the submit button, Drupal will redirect you after submitting the form, so you do not need to reload the page manually. See Post/Redirect/Get for why it is generally a good idea to redirect after a HTTP Post.

1
  • Thanks a lot Oswald. You saved my day. Drupal's Form API worked like a charm.
    – ronnie
    Commented Sep 13, 2011 at 16:49

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.