When you try to add a fourth poll choice the new fields replace the fields for the 3rd choice, so you can never get more than 3.

#7 pollfield.module.patch3.35 KBmr.j
Members fund testing for the Drupal project. Drupal Association Learn more


mr.j’s picture

I should add that I had 2 choices by default.

I increased the default number to 5 or 6 and that works in allowing me to make 5 or 6 choices, but it is not possible to add more by clicking the add choice button.

bryan kennedy’s picture

Priority: Normal » Major

Interesting. I can confirm this bug. How to replicate:

  1. Make a new content type, and add a pollfield
  3. Save the field
  4. Create a new node using this content type.
  5. Add choice #1 and choice #2 and then press the "More choices" button.
  6. A third choice appears. Fill in some text
  7. Press more choices again for a fourth option
  8. The third option will be erased and the fourth option is not added

I'll take a look at this and see if I can figure out what's going on. Obviously this is probably linked to all the default option issues, but I'm glad you brought up this specific problem.

hsimeoni’s picture

Version: 6.x-1.x-dev » 6.x-1.9-beta1
Assigned: Unassigned » hsimeoni
Priority: Major » Critical

same here

I think it is critical for someone using it.
It is a critical functionality

bryan kennedy’s picture

Version: 6.x-1.9-beta1 » 6.x-1.x-dev

I agree it is critical, but I set the version back to dev. This issue presents itself in the latest dev code, and I am focusing on fixing it there. These won't be any work done on 6.x-1.9-beta1.

I haven't abandoned this issue, just have gotten pretty swamped on the weekends lately. I spent a bit of time working on it this week, and I'll update more in a few days.

Hsimeoni, you assigned this issue to yourself. Are you planning on doing some work on this code? I'd welcome any patch submission, but just wanted to check. Cheers.

jordan8037310’s picture

Has any more work been done on this issue? Experiencing the same problem.

lemartialou’s picture

Hi there,

I was experiencing the same problem too (which seems to be a duplicate of this one: https://drupal.org/node/1168408)

So yesterday I decided to take care of myself by diving into tho code... for the win!!! Yaaay! ;p

I'm not a pro drupal/php developer, so maybe I'm doing something wrong, but I tested my solution as much as I could, and it really seems to work as it should.

Also I'm not a patch maker (sorry for that) so I'll just tell you what I did, taking the last 6.x-1.x-dev as a reference.

So, in the file pollfield.module

On line 1051, replace:

$count_choices = max($count_default_response_fields, intval(count($node_choices)));


  if ($node_choices) {
    $count_choices = max($count_default_response_fields, intval(count($node_choices)));
  } else {
    $count_choices = max($count_default_response_fields, intval(count($element['#value']['group'])));

Reason: the variable $node_choices (in fact it's the variable $element['#value']['choice']) doesn't seem to return anything when rebuilding the form after an add_choice action, looking at the 6.x-1.13-beta1 code (which kind of worked) under some condition it used $element['#value']['group'] to do the counting, this worked, but only for the form generated by the add_choice action, so not when $noide_choices is available.

Then, around line 1070:

    // Check to see if the first choice value is empty or not
    // before adding more choices.
    $first_empty = FALSE;
    if ($delta == 0 && drupal_strlen($value_choice) < 1) {
      $first_empty = TRUE;

I simply removed this part, it doesn't seem necessary to me and can lead to problems. We don't need to check if the first choice is empty, the module already check if the last choice is empty to prevent adding more choices, we don't care about the first choice don't we? (well unless there is only one choice maybe, I've not tested this possibility ;p)

With this $first_choice checking, if a user has a poll full of questions, and then for some reason he decides to remove only the first one and leave the others, on his next node edit all the other choices would disappear, with no way to get them back, he only get the first blank choice, and has to override everything.

Then around line 1076:

if (drupal_strlen($value_choice) > 0 || $delta << $count_choices || $first_empty) {

Replace by:

if (drupal_strlen($value_choice) > 0 || $delta < $count_default_response_fields) {

Reason: No need for $first_empty, I think the << is a mistype, and here we want to test if the maximum delta is below the default number of choices, so that when the add_choice form rebuilding takes place it doesn't remove empty choices that should be present.

And finally, around line 1102:

  // Check if this is a "More choices" button submit
  if (strcmp($form_state['clicked_button']['#name'], $nid ."-". $path) == 0) {
    if (drupal_strlen($element['group'][$number-1]['choice']['#default_value'])>0) {
        $element['group'][$number] = array(
          '#type' => 'fieldset',
          '#title' => t('Choice #%delta', array('%delta' => intval($number + 1))),
          '#tree' => TRUE,
        $element['group'][$number]['choice'] = array(
          '#title' => t('Response'),
          '#type' => 'textfield',
          '#default_value' => '',
          '#rows' => 2,
          '#weight' => floatval($pollfield_form['widget']['weight'] + ($delta / 10)),
        $element['group'][$number]['votes'] = array(
          '#title' => t('Starting votes count (optional)'),
          '#access' => $admin_pollfield,
          '#type' => 'textfield',
          '#default_value' => '',
          '#element_validate' => array('_pollfield_is_digits_validate'),
          '#size' => 10,
          '#weight' => floatval($pollfield_form['widget']['weight'] + ($delta / 10) + .1),

Here there are 6 occurences of the variable $number, which doesn't seem to be cited anywhere else, and is not initialized.
Again, looking at the 6.x-1.13-beta1 code I realized that it was a vestige. The $number variable has been replaced by the $i variable in 6.x-dev code (used just above this code).

So just replace those 6 $numbers by 6 $i and everything should be good.

Et voilà! It's all it takes to get the "add choice" button back to work, at least on my site ;)

I hope this will work for you too, don't hesitate to tell me if I'm doing something wrong, and if I'm not I hope there will be some "patchers" around to take care of this problem ;)


mr.j’s picture

Assigned: hsimeoni » Unassigned
3.35 KB

Patch attached. It includes your fixes and also one more to make the pollfield fieldset expanded if it has a question set. i.e. when editing the node it always annoyed me that you had to expand the pollfield fieldset when it had been used.

The patch does solve the problem of adding new poll choices.

However I have noticed a serious bug that seems to be common to AHAH implementations on Drupal 6, as described here:

1. Create or edit an existing node with a pollfield. Leave a required field such as the title or body blank.
2. Fill in all poll choices, and make sure you add some extra choices.
3. Save the node.
4. The page refreshes and you are told to fill in the required fields that you left blank in step 1. Fill in the required fields and save again.
5. The form is submitted to the wrong URL and you will get a white page with what looks like the AHAH response from the last "more choice" request instead of seeing the published node.

This is a common problem because users often leave the body field empty when creating a poll and put everything in the poll question field instead.

The solutions posted in the linked article do not work due I think to this being a CCK-based module. I tried for a few hours and couldn't work it out. This module appears to be abandoned and the readability of its code is terrible so I don't expect that much work will be put into solving this.

lemartialou’s picture

Thank you for making the patch! ^^

To make the fieldset automatically expanded I've found another solution that doesn't rely on the question field, take a look at https://drupal.org/node/2044663 (I tried to resolve some other problems there too)

Now this AHAH problem looks like a pain in the...

mr.j’s picture

A lot of trouble could be saved by just entering poll choices using a single text area with each choice added on a new line, like Drupal does with the block configuration pages. It would take up far less space and less code, there is no AHAH required, and no associated issues.