function poll_form

You are here

7 poll.module poll_form($node, &$form_state)
4.6 poll.module poll_form(&$node)
4.7 poll.module poll_form(&$node)
5 poll.module poll_form($node, $form_values = NULL)
6 poll.module poll_form(&$node, $form_state)

Implementation of hook_form().

File

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

Code

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,
  );

  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);
  }

  $form['choices'] = array(
    '#type' => 'hidden',
    '#value' => $choices,
  );

  // Poll choices
  $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'),
    '#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,
      );
    }
  }

  // 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,
    '#options' => $_duration,
    '#description' => t('After this period, the poll will be closed automatically.'),
  );

  $form['#multistep'] = TRUE;
  return $form;
}