Same name and namespace in other branches
  1. 4.6.x modules/poll.module \poll_form()
  2. 5.x modules/poll/poll.module \poll_form()
  3. 6.x modules/poll/poll.module \poll_form()
  4. 7.x modules/poll/poll.module \poll_form()

Implementation of hook_form().

File

modules/poll.module, line 128
Enables your site to capture votes on different topics in the form of multiple choice questions.

Code

function poll_form(&$node) {
  $admin = user_access('administer nodes');
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Question'),
    '#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,
  );
  $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 the value was changed in a previous iteration, retain it.
  $node->choices = $form['choice']['choices']['#value'];

  // 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'],
    );
    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,
      );
    }
  }

  // Poll attributes
  $_duration = array(
    0 => t('Unlimited'),
  ) + drupal_map_assoc(array(
    86400,
    172800,
    345600,
    604800,
    1209600,
    2419200,
    4838400,
    9676800,
    31536000,
  ), "format_interval");
  $_active = array(
    0 => t('Closed'),
    1 => t('Active'),
  );
  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']['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.'),
  );
  return $form;
}