On my web page, I want a views listing of all my polls. I got the title and the active status on the page but I can't figure out how to get the actual body of the poll on the page, so, for example show results if the person has already voted or showing they can vote if the poll is still active. I added Node Body as a field in my view but it didn't work. Nothing showed up.

Is this possible?

Becky

Comments

merlinofchaos’s picture

Status: Active » Fixed

For polls you must use the 'Node' row style, because Drupal generates poll info on the fly where the "node: body" field just reads what's in the database.

cgillogly’s picture

Would it be possible for Views to create special field that could pull out the "body" generated by the poll module? I'm sure there is a way to have just the "body" rendered so that it could be displayed outside of the actual node, but I'm not sure if Views has the ability to create a special field to do something like that. If it does and someone can point me in the right direction as to how a field like that could be added to Views (documentation page or something), I can take a look at it and maybe try to create a patch to add this.

I would rather see this option be available in the poll module itself to provide views with a field like that, but it could be created in views at first I guess and then moved to poll in core later.

beckyjohnson’s picture

Thanks.

cgillogly’s picture

Rather than writing a patch for views, which I think would be more complicated to do and take much longer, I'm working with the Views Custom Field module (http://drupal.org/project/views_customfield) to do this. Trying to figured out the code to put in the custom field and as soon as I get this working I'll post the result.

cgillogly’s picture

OK. I got it.

First thing you need to do is install the module I mentioned above. Then in your view settings under the Fields section when you add a field there will be a new field group called "Customfield". Select that group and select "Customfield: PHP code" for the field to add. For the field settings after you add it, put the following in the Value box and save:

<?php
$node = node_load($data->nid);
node_view($node);
print $node->content['body']['#value'];
?>

This will make the field output the choices to vote on and the vote button OR the results if they have already voted. As you can probably tell from the code, it's the entire "body" part of the poll output.

merlinofchaos’s picture

Note that doing this is inefficient, since you'll be loading fields *and* performing node_load() on the poll (which is unavoidable without really understanding poll enough to get the right data directly in the query). Yes it woudl be possible to write a custom field to do this. I think. If someone really wants to do it.

IMO your best bet is probably to use node-view-*.tpl.php and make a customized template so you can use the 'node' row style and not have to load any additional fields; just show what you want. This isn't always going to work but if you're just showing data about the node and nearby, it ought to do.

cgillogly’s picture

After I get the website I'm currently working on up and live (hopefully within the next two weeks) I may tackle the custom filed and see if I can get a poll body field available to views. I need to study the poll module a little more to figure out how to just get that part as a field. I was trying to figure that out last night, but couldn't get it so I went with the solution above.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.