I just spent several hours on a client's site tracking down what seemed like either bad data or serious misconfiguration of blocks or views, only to discover that it's actual a critical flaw in Advanced Poll.

The Problem

The client had created several advanced polls a few months ago and had chosen to create them as blocks. The polls were set to expire after a few weeks, and some of the polls were restricted only to show up on certain pages.

Flashing forward to now, the client created a few new pages on site and suddenly several of these older polls started showing up. Except that the poll blocks were nowhere to be found under admin/structure/block, and editing the block nodes themselves didn't seem to indicate any options for blocks at all.

The Root Cause

As it turns out, the issue is that Advanced Poll uses a different query to control the blocks it exposes in hook_block_info() than the one that Drupal uses when rendering pages via block_list() (the query is in _block_load_blocks()). When polls expire, they stop being exposed by advpoll_block_info(), but all that does is hide them from the admin area -- the blocks will continue to appear in pages, except without any way (other than deleting the nodes) to make them stop appearing in blocks on the site.

Here's the query that is invoked in advpoll_get_poll_info_blocks() that drives what Drupal presents to admins (offending code denoted):

  $result = db_query("SELECT n.title, n.nid FROM {node} n
                      LEFT JOIN {field_data_advpoll_dates} d
                      ON d.revision_id = n.vid
                      LEFT JOIN {field_data_advpoll_closed} c
                      ON c.revision_id = n.vid
                      LEFT JOIN {field_data_advpoll_options} o
                      ON o.revision_id = n.vid
                      WHERE
                      n.type = 'advpoll' AND
                      o.advpoll_options_value = 'block' AND
                      n.status = 1 AND
                      c.advpoll_closed_value = 'open' AND
                      d.advpoll_dates_value < NOW()             -- HERE
                      AND d.advpoll_dates_value2 > NOW()     -- HERE
                      ORDER BY n.created DESC");

And here's the query that Drupal uses when rendering blocks (from _block_load_blocs()):

$result = $query
  ->fields('b')
    ->condition('b.theme', $theme_key)
    ->condition('b.status', 1)
    ->orderBy('b.region')
    ->orderBy('b.weight')
    ->orderBy('b.module')
    ->addTag('block_load')
    ->addTag('translatable')
    ->execute();

See the issue? The date restriction hides the blocks from one place, but they still get rendered.

This is so frustrating for a site builder to pin down, especially because both the configuration and the database look correct. It's further compounded by the fact that the block-related settings for the Advanced Polls aren't shown on the nodes after they've been created.

Comments

GuyPaddock created an issue. See original summary.

GuyPaddock’s picture

Issue summary: View changes
GuyPaddock’s picture

Thinking this over, there really is no sense to having open/closed/expired conditionals in this query UNLESS in advpoll_block_view(), Advanced Poll similarly guards against rendering blocks that are disabled.

My vote is never to hide the blocks from the admin, but to simply prevent rendering the blocks for polls that have expired (i.e. move the conditional logic and and into advpoll_block_view().

tripper54’s picture

This is a duplicate of #2153631: Advanced poll is not visible in admin blocks page if poll date has expired., however I think I'll close the other one as the description here is much better.

tripper54’s picture

I agree that expired blocks should not be hidden from the admin block views.

Ideally you should be able to choose the behaviour when the poll closes: either hide the block or display the results.

Patches welcome!

tripper54’s picture

@GuyPaddock, one question. You say:

" It's further compounded by the fact that the block-related settings for the Advanced Polls aren't shown on the nodes after they've been created."

Are you referring to the 'generate a block for this poll' checkbox on the node edit form? If so, that appears for me on a clean install after the node has been created.

If not, what settings do you mean?

GuyPaddock’s picture

@tripper54: I see what you mean. I stand corrected -- I do still have the checkbox for creating the block; I didn't see it previously.

Then again, as part of triaging this issue I did update the version of Advanced Poll the client was using, so perhaps the previous version had a problem with only showing the option on node creation. I am not sure. Either way, you are correct that the option is still there on existing nodes, which is good to know.

For now, as a workaround for this client, I've applied a quick patch to eliminate several conditionals from the query in advpoll_get_poll_info_blocks().

GuyPaddock’s picture

Priority: Major » Normal

Dropping down to Normal now that there is a workaround for the client (just uncheck the box on the node).

GuyPaddock’s picture

Issue summary: View changes
GuyPaddock’s picture

Issue summary: View changes
tripper54’s picture

Version: 7.x-2.x-dev » 7.x-3.x-dev