Skip to content
Snippets Groups Projects

User can stuck on View results

Files

+ 96
31
@@ -66,8 +66,8 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -66,8 +66,8 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
if ($request->isMethod('POST') && $this->poll->hasUserVoted()) {
if ($request->isMethod('POST') && $this->poll->hasUserVoted()) {
$input = $form_state->getUserInput();
$input = $form_state->getUserInput();
if (isset($input['op']) && $input['op'] == $this->t('Vote')) {
if (isset($input['op']) && $input['op'] == $this->t('Vote')) {
// If this happened, then the form submission was likely a cached page.
// If this happened, then the form submission was likely
// Force a session for this user so he can see the results.
// a cached page. Force a session for this user.
$this->messenger()->addError($this->t('Your vote for this poll has already been submitted.'));
$this->messenger()->addError($this->t('Your vote for this poll has already been submitted.'));
$_SESSION['poll_vote'][$this->poll->id()] = FALSE;
$_SESSION['poll_vote'][$this->poll->id()] = FALSE;
}
}
@@ -84,12 +84,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -84,12 +84,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
else {
else {
$options = $this->poll->getOptions();
$options = $this->poll->getOptions();
if ($options) {
if ($options) {
$form['choice'] = array(
$form['choice'] = [
'#type' => 'radios',
'#type' => 'radios',
'#title' => t('Choices'),
'#title' => t('Choices'),
'#title_display' => 'invisible',
'#title_display' => 'invisible',
'#options' => $options,
'#options' => $options,
);
];
}
}
$form['#theme'] = 'poll_vote';
$form['#theme'] = 'poll_vote';
$form['#entity'] = $this->poll;
$form['#entity'] = $this->poll;
@@ -108,15 +108,23 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -108,15 +108,23 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
$form['actions'] = $this->actions($form, $form_state, $this->poll);
$form['actions'] = $this->actions($form, $form_state, $this->poll);
$form['#cache'] = array(
$form['#cache'] = [
'tags' => $this->poll->getCacheTags(),
'tags' => $this->poll->getCacheTags(),
);
];
return $form;
return $form;
}
}
/**
/**
* Ajax callback to replace the poll form.
* Ajax callback to replace the poll form.
 
*
 
* @param array $form
 
* Form.
 
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* Form state.
 
*
 
* @return \Drupal\Core\Ajax\AjaxResponse
 
* Response.
*/
*/
public function ajaxReplaceForm(array $form, FormStateInterface $form_state) {
public function ajaxReplaceForm(array $form, FormStateInterface $form_state) {
// Embed status message into the form.
// Embed status message into the form.
@@ -140,6 +148,17 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -140,6 +148,17 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
}
}
 
/**
 
* Check if we want to show results.
 
*
 
* @param \Drupal\poll\PollInterface $poll
 
* Poll entity.
 
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* Form state.
 
*
 
* @return bool
 
* TRUE if we want to show results.
 
*/
public function showResults(PollInterface $poll, FormStateInterface $form_state) {
public function showResults(PollInterface $poll, FormStateInterface $form_state) {
$account = $this->currentUser();
$account = $this->currentUser();
switch (TRUE) {
switch (TRUE) {
@@ -164,7 +183,20 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -164,7 +183,20 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
}
}
}
}
protected function actions(array $form, FormStateInterface $form_state, $poll) {
/**
 
* Generates action buttons.
 
*
 
* @param array $form
 
* Form.
 
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* Form state.
 
* @param \Drupal\poll\PollInterface $poll
 
* Poll entity.
 
*
 
* @return array
 
* Action buttons.
 
*/
 
protected function actions(array $form, FormStateInterface $form_state, PollInterface $poll) {
$actions = [];
$actions = [];
// Default ajax behavior, use the poll URL for faster submission, this
// Default ajax behavior, use the poll URL for faster submission, this
@@ -173,7 +205,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -173,7 +205,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
$ajax = [
$ajax = [
'callback' => '::ajaxReplaceForm',
'callback' => '::ajaxReplaceForm',
'url' => $this->poll->toUrl(),
'url' => $this->poll->toUrl(),
'options' => ['query' => [FormBuilderInterface::AJAX_FORM_REQUEST => TRUE, 'view_mode' => $form['#view_mode']]]
'options' => [
 
'query' => [
 
FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
 
'view_mode' => $form['#view_mode'],
 
],
 
],
];
];
if ($this->showResults($poll, $form_state)) {
if ($this->showResults($poll, $form_state)) {
@@ -183,16 +220,16 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -183,16 +220,16 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
$actions['cancel']['#type'] = 'submit';
$actions['cancel']['#type'] = 'submit';
$actions['cancel']['#button_type'] = 'primary';
$actions['cancel']['#button_type'] = 'primary';
$actions['cancel']['#value'] = t('Cancel vote');
$actions['cancel']['#value'] = t('Cancel vote');
$actions['cancel']['#submit'] = array('::cancel');
$actions['cancel']['#submit'] = ['::cancel'];
$actions['cancel']['#ajax'] = $ajax;
$actions['cancel']['#ajax'] = $ajax;
$actions['cancel']['#weight'] = '0';
$actions['cancel']['#weight'] = '0';
}
}
if (!$poll->hasUserVoted() && $poll->isOpen() && $poll->getAnonymousVoteAllow()) {
if ($this->isBackAllowed($poll)) {
Please register or sign in to reply
$actions['#type'] = 'actions';
$actions['#type'] = 'actions';
$actions['back']['#type'] = 'submit';
$actions['back']['#type'] = 'submit';
$actions['back']['#button_type'] = 'primary';
$actions['back']['#button_type'] = 'primary';
$actions['back']['#value'] = t('View poll');
$actions['back']['#value'] = t('View poll');
$actions['back']['#submit'] = array('::back');
$actions['back']['#submit'] = ['::back'];
$actions['back']['#ajax'] = $ajax;
$actions['back']['#ajax'] = $ajax;
$actions['back']['#weight'] = '0';
$actions['back']['#weight'] = '0';
}
}
@@ -202,8 +239,8 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -202,8 +239,8 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
$actions['vote']['#type'] = 'submit';
$actions['vote']['#type'] = 'submit';
$actions['vote']['#button_type'] = 'primary';
$actions['vote']['#button_type'] = 'primary';
$actions['vote']['#value'] = t('Vote');
$actions['vote']['#value'] = t('Vote');
$actions['vote']['#validate'] = array('::validateVote');
$actions['vote']['#validate'] = ['::validateVote'];
$actions['vote']['#submit'] = array('::save');
$actions['vote']['#submit'] = ['::save'];
$actions['vote']['#ajax'] = $ajax;
$actions['vote']['#ajax'] = $ajax;
$actions['vote']['#weight'] = '0';
$actions['vote']['#weight'] = '0';
@@ -212,7 +249,7 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -212,7 +249,7 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
$actions['result']['#type'] = 'submit';
$actions['result']['#type'] = 'submit';
$actions['result']['#button_type'] = 'primary';
$actions['result']['#button_type'] = 'primary';
$actions['result']['#value'] = t('View results');
$actions['result']['#value'] = t('View results');
$actions['result']['#submit'] = array('::result');
$actions['result']['#submit'] = ['::result'];
$actions['result']['#ajax'] = $ajax;
$actions['result']['#ajax'] = $ajax;
$actions['result']['#weight'] = '1';
$actions['result']['#weight'] = '1';
}
}
@@ -226,13 +263,16 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -226,13 +263,16 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
*
*
* @param \Drupal\poll\PollInterface $poll
* @param \Drupal\poll\PollInterface $poll
* The poll entity.
* The poll entity.
 
* @param string $view_mode
 
* View mode.
* @param bool $block
* @param bool $block
* (optional) TRUE if a poll should be displayed in a block. Defaults to
* (optional) TRUE if a poll should be displayed in a block. Defaults to
* FALSE.
* FALSE.
*
*
* @return array $output
* @return array
 
* Poll results.
*/
*/
function showPollResults(PollInterface $poll, $view_mode = 'default', $block = FALSE) {
public function showPollResults(PollInterface $poll, $view_mode = 'default', $block = FALSE) {
// Ensure that a page that shows poll results can not be cached.
// Ensure that a page that shows poll results can not be cached.
\Drupal::service('page_cache_kill_switch')->trigger();
\Drupal::service('page_cache_kill_switch')->trigger();
@@ -243,30 +283,30 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -243,30 +283,30 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
}
}
$options = $poll->getOptions();
$options = $poll->getOptions();
$poll_results = array();
$poll_results = [];
foreach ($poll->getVotes() as $pid => $vote) {
foreach ($poll->getVotes() as $pid => $vote) {
$percentage = round($vote * 100 / max($total_votes, 1));
$percentage = round($vote * 100 / max($total_votes, 1));
$display_votes = (!$block) ? ' (' . \Drupal::translation()
$display_votes = (!$block) ? ' (' . \Drupal::translation()
->formatPlural($vote, '1 vote', '@count votes') . ')' : '';
->formatPlural($vote, '1 vote', '@count votes') . ')' : '';
$poll_results[] = array(
$poll_results[] = [
'#theme' => 'poll_meter',
'#theme' => 'poll_meter',
'#choice' => $options[$pid],
'#choice' => $options[$pid],
'#display_value' => t('@percentage%', array('@percentage' => $percentage)) . $display_votes,
'#display_value' => t('@percentage%', ['@percentage' => $percentage]) . $display_votes,
'#min' => 0,
'#min' => 0,
'#max' => $total_votes,
'#max' => $total_votes,
'#value' => $vote,
'#value' => $vote,
'#percentage' => $percentage,
'#percentage' => $percentage,
'#attributes' => array('class' => array('bar')),
'#attributes' => ['class' => ['bar']],
'#poll' => $poll,
'#poll' => $poll,
);
];
}
}
/** @var \Drupal\poll\PollVoteStorageInterface $vote_storage */
/** @var \Drupal\poll\PollVoteStorageInterface $vote_storage */
$vote_storage = \Drupal::service('poll_vote.storage');
$vote_storage = \Drupal::service('poll_vote.storage');
$user_vote = $vote_storage->getUserVote($poll);
$user_vote = $vote_storage->getUserVote($poll);
$output = array(
$output = [
'#theme' => 'poll_results',
'#theme' => 'poll_results',
'#raw_question' => $poll->label(),
'#raw_question' => $poll->label(),
'#results' => $poll_results,
'#results' => $poll_results,
@@ -275,14 +315,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -275,14 +315,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
'#pid' => $poll->id(),
'#pid' => $poll->id(),
'#poll' => $poll,
'#poll' => $poll,
'#view_mode' => $view_mode,
'#view_mode' => $view_mode,
'#vote' => isset($user_vote['chid']) ? $user_vote['chid'] : NULL,
'#vote' => $user_vote['chid'] ?? NULL,
);
];
return $output;
return $output;
}
}
/**
/**
* Cancel vote submit function.
* Cancel vote submit function.
*
*
@@ -295,10 +333,10 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -295,10 +333,10 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
/** @var \Drupal\poll\PollVoteStorageInterface $vote_storage */
/** @var \Drupal\poll\PollVoteStorageInterface $vote_storage */
$vote_storage = \Drupal::service('poll_vote.storage');
$vote_storage = \Drupal::service('poll_vote.storage');
$vote_storage->cancelVote($this->poll, $this->currentUser());
$vote_storage->cancelVote($this->poll, $this->currentUser());
\Drupal::logger('poll')->notice('%user\'s vote in Poll #%poll deleted.', array(
\Drupal::logger('poll')->notice('%user\'s vote in Poll #%poll deleted.', [
'%user' => $this->currentUser()->id(),
'%user' => $this->currentUser()->id(),
'%poll' => $this->poll->id(),
'%poll' => $this->poll->id(),
));
]);
$this->messenger()->addMessage($this->t('Your vote was cancelled.'));
$this->messenger()->addMessage($this->t('Your vote was cancelled.'));
// In case of an ajax submission, trigger a form rebuild so that we can
// In case of an ajax submission, trigger a form rebuild so that we can
@@ -312,7 +350,9 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -312,7 +350,9 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
* View vote results submit function.
* View vote results submit function.
*
*
* @param array $form
* @param array $form
 
* Form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* Form state.
*/
*/
public function result(array $form, FormStateInterface $form_state) {
public function result(array $form, FormStateInterface $form_state) {
$form_state->set('show_results', TRUE);
$form_state->set('show_results', TRUE);
@@ -323,7 +363,9 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -323,7 +363,9 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
* Back to poll view submit function.
* Back to poll view submit function.
*
*
* @param array $form
* @param array $form
 
* Form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* Form state.
*/
*/
public function back(array $form, FormStateInterface $form_state) {
public function back(array $form, FormStateInterface $form_state) {
$form_state->set('show_results', FALSE);
$form_state->set('show_results', FALSE);
@@ -334,10 +376,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -334,10 +376,12 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
* Save a user's vote submit function.
* Save a user's vote submit function.
*
*
* @param array $form
* @param array $form
 
* Form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* Form state.
*/
*/
public function save(array $form, FormStateInterface $form_state) {
public function save(array $form, FormStateInterface $form_state) {
$options = array();
$options = [];
$options['chid'] = $form_state->getValue('choice');
$options['chid'] = $form_state->getValue('choice');
$options['uid'] = $this->currentUser()->id();
$options['uid'] = $this->currentUser()->id();
$options['pid'] = $form_state->getValue('poll')->id();
$options['pid'] = $form_state->getValue('poll')->id();
@@ -373,7 +417,9 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -373,7 +417,9 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
* Validates the vote action.
* Validates the vote action.
*
*
* @param array $form
* @param array $form
 
* Form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* @param \Drupal\Core\Form\FormStateInterface $form_state
 
* Form state.
*/
*/
public function validateVote(array &$form, FormStateInterface $form_state) {
public function validateVote(array &$form, FormStateInterface $form_state) {
if (!$form_state->hasValue('choice')) {
if (!$form_state->hasValue('choice')) {
@@ -385,6 +431,7 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -385,6 +431,7 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
* Checks if the current user is allowed to cancel on the given poll.
* Checks if the current user is allowed to cancel on the given poll.
*
*
* @param \Drupal\poll\PollInterface $poll
* @param \Drupal\poll\PollInterface $poll
 
* Poll entity.
*
*
* @return bool
* @return bool
* TRUE if the user can cancel.
* TRUE if the user can cancel.
@@ -396,10 +443,28 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
@@ -396,10 +443,28 @@ class PollViewForm extends FormBase implements BaseFormIdInterface {
&& $poll->getCancelVoteAllow()
&& $poll->getCancelVoteAllow()
// And the user has the cancel own vote permission.
// And the user has the cancel own vote permission.
&& $this->currentUser()->hasPermission('cancel own vote')
&& $this->currentUser()->hasPermission('cancel own vote')
// And the user is authenticated or his session contains the voted flag.
// And the user is authenticated or session contains the voted flag.
&& (\Drupal::currentUser()->isAuthenticated() || !empty($_SESSION['poll_vote'][$poll->id()]))
&& (\Drupal::currentUser()->isAuthenticated() || !empty($_SESSION['poll_vote'][$poll->id()]))
// And poll is open.
// And poll is open.
&& $poll->isOpen();
&& $poll->isOpen();
}
}
 
/**
 
* Checks if the current user is allowed to go back.
 
*
 
* @param \Drupal\poll\PollInterface $poll
 
* Poll entity.
 
*
 
* @return bool
 
* TRUE if the user can cancel.
 
*/
 
protected function isBackAllowed(PollInterface $poll) {
 
// User not voted yet.
 
return !$poll->hasUserVoted()
 
// And the poll is open.
 
&& $poll->isOpen()
 
// Anonymous vote allowed or user is logged in.
 
&& ($poll->getAnonymousVoteAllow() || $this->currentUser()->isAuthenticated());
 
}
    • ... The new helper. The change is at the last condition, which is grouped with a new $this->currentUser()->isAuthenticated() chained with OR. Since there is no dedicated permission (or any equivalent feature) for authenticated user accounts, this is the solution we can use here :thumbsup:

Please register or sign in to reply
 
}
}
Loading