From 5dc27bcb93db939584576194414e33d330cb7b38 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Wed, 30 Aug 2006 18:50:35 +0000
Subject: [PATCH] - Patch #80860 by eaton et al: use form API multistep for
 poll.module

---
 includes/form.inc        |   5 --
 modules/node/node.module |   4 +-
 modules/poll/poll.module | 100 ++++++++++++++++++++++++++++++---------
 3 files changed, 79 insertions(+), 30 deletions(-)

diff --git a/includes/form.inc b/includes/form.inc
index 40ab647cee01..d5607d957a16 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -99,11 +99,6 @@ function drupal_get_form($form_id) {
       $_SESSION['form'][$form_build_id] = $args;
       $form['#build_id'] = $form_build_id;
     }
-    // If we're in this part of the code, $_POST always contains
-    // values from the previously submitted form. Unset it to avoid
-    // any accidental submission of doubled data, then process the form
-    // to prep it for rendering.
-    unset($_POST);
     drupal_process_form($args[0], $form);
   }
 
diff --git a/modules/node/node.module b/modules/node/node.module
index 3364aa3a2934..149580839995 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -1820,7 +1820,7 @@ function node_object_prepare(&$node) {
 /**
  * Generate the node add/edit form array.
  */
-function node_form($node) {
+function node_form($node, $form_values = NULL) {
   global $user;
 
   $node = (object)$node;
@@ -1841,7 +1841,7 @@ function node_form($node) {
   $form['changed'] = array('#type' => 'hidden', '#value' => $node->changed);
 
   // Get the node-specific bits.
-  $form = array_merge_recursive($form, node_invoke($node, 'form'));
+  $form = array_merge_recursive($form, node_invoke($node, 'form', $form_values));
   if (!isset($form['title']['#weight'])) {
     $form['title']['#weight'] = -5;
   }
diff --git a/modules/poll/poll.module b/modules/poll/poll.module
index 7e801701b50c..317c6ce8711b 100644
--- a/modules/poll/poll.module
+++ b/modules/poll/poll.module
@@ -123,32 +123,67 @@ function poll_form_alter($form_id, &$form) {
 /**
  * Implementation of hook_form().
  */
-function poll_form(&$node) {
+function poll_form($node, $form_values = NULL) {
   $admin = user_access('administer nodes');
   $type = node_get_types('type', $node);
+  $form['title'] = array(
+    '#type' => 'textfield',
+    '#title' => check_plain($type->title_label),
+    '#required' => TRUE,
+    '#default_value' => $node->title,
+    '#weight' => -1
+  );
 
-  $form['title'] = array('#type' => 'textfield', '#title' => check_plain($type->title_label), '#required' => TRUE, '#default_value' => $node->title, '#weight' => -1);
-
-  $form['choice']['choices'] = array('#type' => 'hidden', '#default_value' => max(2, count($node->choice) ? count($node->choice) : 5));
-  $form['choice']['morechoices'] = array('#type' => 'checkbox', '#title' => t('Need more choices'), '#default_value' => 0, '#description' => t("If the amount of boxes above isn't enough, check this box and click the Preview button below to add some more."), '#weight' => 1);
-  // when building the whole form, #post is overwritten unconditionally,
-  // so no problems from setting by hand here for the local build
-  $form['choice']['#post'] = $_POST;
-  $form['choice'] = form_builder('poll_node_form', $form['choice']);
-  if ($form['choice']['morechoices']['#value']) {
-    $form['choice']['morechoices']['#value'] = 0;
-    $form['choice']['choices']['#value'] *= 2;
+  if (isset($form_values)) {
+    $choices = $form_values['choices'];
+    if ($form_values['morechoices']) {
+      $choices *= 2;
+    }
+  }
+  else {
+    $choices = max(2, count($node->choice) ? count($node->choice) : 5);
   }
 
-  // if the value was changed in a previous iteration, retain it.
-  $node->choices = $form['choice']['choices']['#value'];
+  $form['choices'] = array(
+    '#type' => 'hidden',
+    '#default_value' => $choices,
+  );
 
   // Poll choices
-  $form['choice'] += array('#type' => 'fieldset', '#title' => t('Choices'), '#prefix' => '<div class="poll-form">', '#suffix' => '</div>', '#tree' => TRUE);
-  for ($a = 0; $a < $node->choices; $a++) {
-    $form['choice'][$a]['chtext'] = array('#type' => 'textfield', '#title' => t('Choice @n', array('@n' => ($a + 1))), '#default_value' => $node->choice[$a]['chtext']);
+  $form['choice'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Choices'),
+    '#prefix' => '<div class="poll-form">',
+    '#suffix' => '</div>',
+    '#tree' => TRUE
+  );
+
+  // We'll manually set the #parents property of this checkbox so that
+  // it appears in the fieldset visually, but its value won't pollute
+  // the $form_values['choice'] array.
+  $form['choice']['morechoices'] = array(
+    '#type' => 'checkbox',
+    '#parents' => array('morechoices'),
+    '#title' => t('Need more choices'),
+    '#default_value' => 0,
+    '#description' => t("If the amount of boxes above isn't enough, check this box and click the Preview button below to add some more."),
+    '#weight' => 1,
+  );
+
+  for ($a = 0; $a < $choices; $a++) {
+    $form['choice'][$a]['chtext'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Choice @n', array('@n' => ($a + 1))),
+      '#default_value' => $node->choice[$a]['chtext'],
+    );
+
     if ($admin) {
-      $form['choice'][$a]['chvotes'] = array('#type' => 'textfield', '#title' => t('Votes for choice @n', array('@n' => ($a + 1))), '#default_value' => (int)$node->choice[$a]['chvotes'], '#size' => 5, '#maxlength' => 7);
+      $form['choice'][$a]['chvotes'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Votes for choice @n', array('@n' => ($a + 1))),
+        '#default_value' => (int)$node->choice[$a]['chvotes'],
+        '#size' => 5, '#maxlength' => 7
+      );
     }
   }
 
@@ -158,10 +193,24 @@ function poll_form(&$node) {
 
   if ($admin) {
     $form['settings'] = array('#type' => 'fieldset', '#title' => t('Settings'));
-    $form['settings']['active'] = array('#type' => 'radios', '#title' => t('Poll status'), '#default_value' => isset($node->active) ? $node->active : 1, '#options' => $_active, '#description' => t('When a poll is closed, visitors can no longer vote for it.'));
+
+    $form['settings']['active'] = array(
+      '#type' => 'radios',
+      '#title' => t('Poll status'),
+      '#default_value' => isset($node->active) ? $node->active : 1,
+      '#options' => $_active,
+      '#description' => t('When a poll is closed, visitors can no longer vote for it.')
+    );
   }
-  $form['settings']['runtime'] = array('#type' => 'select', '#title' => t('Poll duration'), '#default_value' => $node->runtime ? $node->runtime : 0, '#options' => $_duration, '#description' => t('After this period, the poll will be closed automatically.'));
+  $form['settings']['runtime'] = array(
+    '#type' => 'select',
+    '#title' => t('Poll duration'),
+    '#default_value' => $node->runtime,
+    '#options' => $_duration,
+    '#description' => t('After this period, the poll will be closed automatically.'),
+  );
 
+  $form['#multistep'] = TRUE;
   return $form;
 }
 
@@ -324,13 +373,18 @@ function poll_teaser($node) {
 /**
  * Generates the voting form for a poll.
  */
-function poll_view_voting($node, $page) {
+function poll_view_voting($node, $block) {
   if ($node->choice) {
     $list = array();
     foreach ($node->choice as $i => $choice) {
       $list[$i] = check_plain($choice['chtext']);
     }
-    $form['choice'] = array('#type' => 'radios', '#title' => $page ? '' : check_plain($node->title), '#default_value' => -1, '#options' => $list);
+    $form['choice'] = array(
+      '#type' => 'radios',
+      '#title' => $block ? check_plain($node->title) : '',
+      '#default_value' => -1,
+      '#options' => $list,
+    );
   }
   $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
   $form['vote'] = array('#type' => 'submit', '#value' => t('Vote'));
@@ -571,7 +625,7 @@ function poll_view($node, $teaser = FALSE, $page = FALSE, $block = FALSE) {
       poll_vote($node);
     }
     $node->content['body'] = array(
-      '#value' => drupal_get_form('poll_view_voting', $node, $page),
+      '#value' => drupal_get_form('poll_view_voting', $node, $block),
     );
   }
   else {
-- 
GitLab