It would be useful to be able to display a specific poll in a block/widget that can be embedded into pages, perhaps with an option to AJAX-load the results into the block (i.e. instead of redirecting the user to the poll page after they respond).

The current functionality (most recent poll block) doesn't allow for the possibility of there being multiple polls in use on a site.

Comments

jeremylichtman created an issue. See original summary.

jeremylichtman’s picture

jeremylichtman’s picture

Issue summary: View changes
berdir’s picture

I don't understand.

The module provides a block that shows the latest poll. Since there is views support, you could also build a view that would show a poll based on whatever conditions you want instead.

The module shouldn't redirect to the detail page. It submits there, but it uses ajax already and also sets a destination, to make sure that the user stays where he is.

jeremylichtman’s picture

Let's say you have five polls currently running, and you want to display them in different parts of a (large) site, and you want a content editor (not coder or super admin) to be able to maintain them (and they've had a bit of training in how to use blocks and widgets).

On our end, we're leaning towards creating a custom widget that leverages the poll module, but this is a coding task.

Wouldn't it be easier to have a poll block with a form field that allows specifying which poll to display?

berdir’s picture

There are pretty easy ways to just that. For example, create a poll content block type with a reference field and allow to reference and display a view through that.

I'm not sure about adding more hardcoded blocks, so leaving this open to see if more users are interested in that.

The ajax/redirect problems have been fixed elsewhere and things are working pretty well now.

trwill’s picture

I've done this - will work to submit a patch today at the sprint

trwill’s picture

Here it is - first attempt at a block for selecting a poll.

blakehall’s picture

Status: Active » Needs review

This patch looks like it should be ready for review.

berdir’s picture

Status: Needs review » Needs work

Thanks for starting this, there's quite a bit of cleanup necessary.

  1. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,106 @@
    +   * {@inheritdoc}
    +   */
    +  protected function blockAccess(AccountInterface $account) {
    +	return AccessResult::allowedIfHasPermission($account, 'access polls');
    +  }
    

    Needs to use two spaces instead of tabs (everywhere, not just here)

  2. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,106 @@
    +  public function build() {
    +	$config = $this->getConfiguration();
    +	$poll_lookup = isset($config['poll_lookup']) ? $config['poll_lookup'] : '';
    +	$poll_title = isset($config['poll_title']) ? $config['poll_title'] : 'Poll';
    

    Default configuration belongs in the defaultConfiguration() method.

  3. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,106 @@
    +	$query = \Drupal::entityQuery('poll');
    +	$query->condition('id', $poll_lookup, '=');
    +	$polls = \Drupal::entityTypeManager()->getStorage('poll')->loadMultiple($query->execute());
    +
    

    You already have an ID. Just load it. You're doing a query to get the ID based on the ID.

  4. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,106 @@
    +	  $output['#title'] = $poll_title;
    +	  $output['#prefix'] = "<h3 class='poll-question'>" . $poll->label() . "</h3>";
    

    hardcoded markup doesn't belong here.

    Each block already has a title, no need to duplicate that with another configurable title.

  5. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,106 @@
    +	// Set a default to be most recent poll in block.
    +	$default_poll = \Drupal::entityTypeManager()
    +	  ->getStorage('poll')
    +	  ->getMostRecentPoll();
    +
    +	// If there is already a poll set, ensure it appears as default.
    +	$current_poll_id = isset($config['poll_lookup']) ? $config['poll_lookup'] : FALSE;
    +	if ($current_poll_id) {
    +	  $form_query = \Drupal::entityQuery('poll');
    +	  $form_query->condition('id', $current_poll_id, '=')->pager(1);
    +
    +	  $default_poll = \Drupal::entityTypeManager()
    +		->getStorage('poll')
    +		->loadMultiple($form_query->execute());
    +	}
    +
    +	// Run a reset() to ensure arr set to proper index.
    +	$default_poll_obj = reset($default_poll);
    

    This is also all way too complicated.

    Either you have an ID, or if you don't, you load the most recent.

    Then you can simplify this to a if (!$current_poll_id) { load latest

  6. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,106 @@
    +	  '#default_value' => $default_poll_obj,
    

    afaik this accepts either an ID or an entity, so you don't even have to load it. Not 100% sure about that, though.

prashant114606’s picture

Let's assume we need more than one poll to display it on different part of the working sites. You can make use of the contrib module called NodeBlock(https://www.drupal.org/project/nodeblock). When you enable the core module poll it gives a content types called poll under which there a setting option will be available once you will enable the NodeBlock module. Configure it according to your need and you can make all the poll available as block which can be placed on different part of site as per need.

berdir’s picture

See #6. You can easily replicate that in D8 by creating blocks with a reference to a poll.

Another possibility would be ctools entity view blocks, but then you need something that exposes the poll as context so it can use it. Easy with static context in page manager/panels but currently not possible for normal blocks.

trwill’s picture

This block allows specified polls to be placed in different parts of the site, multiple times. One poll per block can be placed anywhere, multiple times, even using something like Panelizr. I have not given up on this patch and just have been very busy. My apologies for spacing and syntax issues, most of this was copied and pasted (which I think explains the tabs) from a site that I had implemented it on. This included the need for an add'l title (obviously not necessary here). I do want to clean this up and appreciate you taking the time to comment on it @Berdir

trwill’s picture

Updated from comments - I don't quite exactly follow w/ default config in #2, so if that's still incorrect lmk and I can update.

trwill’s picture

Status: Needs work » Needs review
trwill’s picture

Assigned: Unassigned » trwill
berdir’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks for working on this.

  1. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,86 @@
    +/**
    + * @file
    + * Contains \Drupal\poll\Plugin\Block\PollSelectBlock.
    + */
    +
    +namespace Drupal\poll\Plugin\Block;
    

    @file should no longer be added to class files.

  2. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,86 @@
    +    $poll_lookup = isset($config['poll_lookup']) ? $config['poll_lookup'] : '';
    +    $poll = \Drupal::entityTypeManager()->getStorage('poll')->load($poll_lookup);
    +
    +	$output = array();
    

    Would be nice to use injection instead of calling \Drupal

    Since both blocks use this, we could make a PollBlockBase class with the inection.

  3. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,86 @@
    +	$output = array();
    +	if ($poll) {
    +	  $view_builder = \Drupal::entityTypeManager()->getViewBuilder('poll');
    +	  $output = $view_builder->view($poll, 'block');
    +    $output['#title'] = $poll->label();
    

    Still lots of tabs.

  4. +++ b/src/Plugin/Block/PollSelectBlock.php
    @@ -0,0 +1,86 @@
    +    $form['poll_lookup'] = array(
    

    poll_lookup is a strange name for this, maybe just poll, poll_id or selected_poll?

This will also need tests.

bramdriesen’s picture

Title: Show polls in a block » Add a polls select block
Version: 8.x-1.x-dev » 2.0.x-dev
Assigned: trwill » Unassigned

If this is still a feature people want, it should be re-rolled into a MR and the latest code review remarks should be implemented as well.