poll_form

Versions
4.6 – 4.7
poll_form(&$node)
5
poll_form($node, $form_values = NULL)
6
poll_form(&$node, $form_state)
7
poll_form($node, &$form_state)

Implement hook_form().

Code

modules/poll/poll.module, line 220

<?php
function poll_form($node, &$form_state) {
  global $user;

  $admin = user_access('administer nodes') || user_access('edit any poll content') || (user_access('edit own poll content') && $user->uid == $node->uid);

  $type = node_type_get_type($node);

  $form_state['cache'] = TRUE;

  if (isset($form_state['choice_count'])) {
    $choice_count = $form_state['choice_count'];
  }
  else {
    $choice_count = max(2, empty($node->choice) ? 2 : count($node->choice));
  }

  // Add a wrapper for the choices and more button.
  $form['choice_wrapper'] = array(
    '#tree' => FALSE,
    '#weight' => -4,
    '#prefix' => '<div class="clearfix" id="poll-choice-wrapper">',
    '#suffix' => '</div>',
  );

  // Container for just the poll choices.
  $form['choice_wrapper']['choice'] = array(
    '#prefix' => '<div id="poll-choices">',
    '#suffix' => '</div>',
    '#theme' => 'poll_choices',
  );

  // Add the current choices to the form.
  $delta = 0;
  $weight = 0;
  if (isset($node->choice)) {
    $delta = count($node->choice);
    $weight = -$delta;
    foreach ($node->choice as $chid => $choice) {
      $key = 'chid:' . $chid;
      $form['choice_wrapper']['choice'][$key] = _poll_choice_form($key, $choice['chid'], $choice['chtext'], $choice['chvotes'], $choice['weight'], $choice_count);
      $weight = ($choice['weight'] > $weight) ? $choice['weight'] : $weight;
    }
  }

  // Add initial or additional choices.
  $existing_delta = $delta;
  for ($delta; $delta < $choice_count; $delta++) {
    $key = 'new:' . ($delta - $existing_delta);
    $form['choice_wrapper']['choice'][$key] = _poll_choice_form($key, NULL, '', 0, $weight, $choice_count);
  }

  // We name our button 'poll_more' to avoid conflicts with other modules using
  // AJAX-enabled buttons with the id 'more'.
  $form['choice_wrapper']['poll_more'] = array(
    '#type' => 'submit',
    '#value' => t('More choices'),
    '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
    '#weight' => 1,
    '#submit' => array('poll_more_choices_submit'), // If no javascript action.
    '#ajax' => array(
      'callback' => 'poll_choice_js',
      'wrapper' => 'poll-choices',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  // Poll attributes
  $duration = array(
    // 1-6 days.
    86400, 2 * 86400, 3 * 86400, 4 * 86400, 5 * 86400, 6 * 86400,
    // 1-3 weeks (7 days).
    604800, 2 * 604800, 3 * 604800,
    // 1-3,6,9 months (30 days).
    2592000, 2 * 2592000, 3 * 2592000, 6 * 2592000, 9 * 2592000,
    // 1 year (365 days).
    31536000,
  );
  $duration = array(0 => t('Unlimited')) + drupal_map_assoc($duration, 'format_interval');
  $active = array(0 => t('Closed'), 1 => t('Active'));

  $form['settings'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#title' => t('Poll settings'),
    '#weight' => -3,
    '#access' => $admin,
  );

  $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.'),
    '#access' => $admin,
  );
  $form['settings']['runtime'] = array(
    '#type' => 'select',
    '#title' => t('Poll duration'),
    '#default_value' => isset($node->runtime) ? $node->runtime : 0,
    '#options' => $duration,
    '#description' => t('After this period, the poll will be closed automatically.'),
  );

  return $form;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.