Problem/Motivation
Poll results always show vote counts: the "Total votes: N" line under the bars and the "(N votes)" suffix on each choice. Site owners who run polls with a small audience do not want visitors to see that only a handful of people voted. Today the only way to hide the counts is a theme override of both templates, and the per-choice counts still add up to the total.
Steps to reproduce
- Create a poll with two choices and vote on it.
- View the poll results.
- The page shows "Total votes: 1" and "100% (1 vote)". There is no setting to hide these numbers.
Proposed resolution
Let the author of a poll decide when vote counts are shown, per poll, with two new base fields on the poll entity next to "Allow view results":
- Display vote count: Never / Always / When the poll has at least a number of votes. Default: Always, so existing polls keep their current output.
- Minimum votes to display counts: integer, at least 1, required and visible only when the threshold mode is selected.
When the counts are hidden, PollViewForm::showPollResults() passes NULL as votes to poll_results and leaves the percentage alone in display_value for each poll_meter. The poll-results.html.twig template only prints the "Total votes" line when votes is not null. Percentages stay visible in all modes. The block display follows the same setting.
New methods on PollInterface: getVoteCountDisplay(), setVoteCountDisplay(), getVoteCountThreshold(), setVoteCountThreshold() and isVoteCountDisplayed(?int $total_votes = NULL): bool, plus the constants VOTE_COUNT_DISPLAY_NEVER, VOTE_COUNT_DISPLAY_ALWAYS and VOTE_COUNT_DISPLAY_THRESHOLD. poll_update_11002() installs the two field storage definitions on existing sites.
Remaining tasks
Waiting for tests to be fixed in #3620843.
User interface changes
Two new fields on the poll add/edit form: "Display vote count" and "Minimum votes to display counts". Poll results omit the vote counts when the poll says so.
API changes
Five new methods and three constants on PollInterface. The votes variable of the poll_results theme hook can be NULL.
Data model changes
Two new base fields on the poll entity: vote_count_display (list_string) and vote_count_threshold (integer).
Issue fork poll-3620831
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
kasperg commented