masthead image

Technology Blog

Showing A Specific Poll In Drupal

Wednesday May 6 2009, 07:31 AM

I'm very surprised Drupal's poll module isn't more flexible. One of the more common things Drupal sites want to accomplish is showing a poll on their web pages. But what good is it if you can only show the latest poll? Not very. This article shows how you can choose any poll to be show in a page.

First, the code:

<div style="float: left; width: 240px; height: 200px; padding: 1em; margin: 1em; border: 1px solid #ccc;">
<?php
  $poll
= node_load(100);
 
$poll = poll_view($poll,TRUE, FALSE, TRUE);
  echo
drupal_render($poll->content);
?>

</div>

Cut and paste this any any of your stories, blogs, pages, or content types where you want a particular poll to be shown. The value in the node_load (here 100) is the node number (node/100). In some of the samples I've seen on various websites, they got the code all wrong and don't even bother to call the drupal_render function either. I basically just pulled this out straight from the poll.module file.

Next, comes the tricky part. Since you are inserting PHP code directly inside a content type, make sure you create an input format that is appropriate. Call it "HTML with PHP" and tick off the following:

Roles

  • Administrator
  • Editor user

Filters

  • HTML corrector
  • Line break converter
  • PHP evaluator
  • URL filter

If you don't have an editor user, go create one and assign the appropriate rights. This makes these users not have full administrator rights and only for editing of node types.

The filters are standard types. However, you must rearrange the order or you will not get the desired results. Click the Rearrange tab and put the filters in the following order:

  1. PHP evaluator
  2. Line break converter
  3. URL filter
  4. HTML corrector

This works for me.