diff --git a/poll.install b/poll.install index 20fa756..790e1d8 100644 --- a/poll.install +++ b/poll.install @@ -125,3 +125,17 @@ function poll_update_8001() { $field_schema['poll__choice']['indexes']['choice_target_id'] = ['choice_target_id']; \Drupal::keyValue('entity.storage_schema.sql')->set('poll.field_schema_data.choice', $field_schema); } + +/** + * Add new field to poll_choice table. + */ +function poll_update_8002() { + $spec = [ + 'type' => 'int', + 'description' => "Fake Votes", + 'length' => 10, + 'not null' => FALSE, + ]; + $schema = \Drupal::database()->schema(); + $schema->addField('poll_choice', 'fake_votes', $spec); +} diff --git a/src/Form/PollForm.php b/src/Form/PollForm.php index fa252cd..52d3d2e 100644 --- a/src/Form/PollForm.php +++ b/src/Form/PollForm.php @@ -60,6 +60,15 @@ class PollForm extends ContentEntityForm { $insert = (bool) $poll->id(); $poll->save(); if ($insert) { + $choices = $poll->get('choice')->getValue(); + foreach ($choices as $choice) { + $query = \Drupal::database()->update('poll_choice'); + $query->fields([ + 'fake_votes' => $choice['fake_votes'], + ]); + $query->condition('id', $choice['target_id']); + $query->execute(); + } $this->messenger()->addMessage($this->t('The poll %poll has been updated.', array('%poll' => $poll->label()))); } else { diff --git a/src/Plugin/Field/FieldWidget/PollChoiceDefaultWidget.php b/src/Plugin/Field/FieldWidget/PollChoiceDefaultWidget.php index 8036682..8d1eb0c 100644 --- a/src/Plugin/Field/FieldWidget/PollChoiceDefaultWidget.php +++ b/src/Plugin/Field/FieldWidget/PollChoiceDefaultWidget.php @@ -63,6 +63,21 @@ class PollChoiceDefaultWidget extends WidgetBase { '#default_value' => $choice ? $choice->choice->value : NULL, '#prefix' => '
', ); + + if ($choice) { + $query = \Drupal::database()->select('poll_choice', 'pc'); + $query->addField('pc', 'fake_votes'); + $query->condition('pc.id', $choice->id()); + $fake_votes = $query->execute()->fetchField(); + + $element['fake_votes'] = array( + '#type' => 'textfield', + '#maxlength' => 10, + '#default_value' => $fake_votes > 0 ? $fake_votes : 0, + '#prefix' => '
', + ); + } + return $element; } diff --git a/src/PollVoteStorage.php b/src/PollVoteStorage.php index ae8dc10..af147bc 100644 --- a/src/PollVoteStorage.php +++ b/src/PollVoteStorage.php @@ -123,7 +123,12 @@ class PollVoteStorage implements PollVoteStorageInterface { $result = $this->connection->query("SELECT chid, COUNT(chid) AS votes FROM {poll_vote} WHERE pid = :pid GROUP BY chid", array(':pid' => $poll->id())); // Replace the count for options that have recorded votes in the database. foreach ($result as $row) { - $votes[$row->chid] = $row->votes; + $query = \Drupal::database()->select('poll_choice', 'pc'); + $query->addField('pc', 'fake_votes'); + $query->condition('pc.id', $row->chid); + $fake_votes = $query->execute()->fetchField(); + + $votes[$row->chid] = $row->votes + $fake_votes; } return $votes;