I've created a view in order to display two pages : last polls, and most voted polls.
For the second one, I need to access in views_ui to the field chvotes of table poll_choice to compute the total amount of votes (using agregation), my sort criterion.
But the fields of poll_choice aren't available in views_ui (not declared in poll.views.inc poll_views_data()).

To do this I've implemented the hook_views_data() hook in a custom module, following the example given in http://api.drupal.org/api/views/views.api.php/function/hook_views_data/7 :

function mymodule_views_data() {
/*
CREATE TABLE `poll_choice` (
 `chid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique identifier for a poll choice.',
 `nid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The node.nid this choice belongs to.',
 `chtext` varchar(128) NOT NULL DEFAULT '' COMMENT 'The text for this choice.',
 `chvotes` int(11) NOT NULL DEFAULT '0' COMMENT 'The total number of votes this choice has received by all users.',
 `weight` int(11) NOT NULL DEFAULT '0' COMMENT 'The sort order of this choice among all choices for the same node.',
 PRIMARY KEY (`chid`),
 KEY `nid` (`nid`)
) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8 COMMENT='Stores information about all choices for all polls.'
*/

  $data['poll_choice']['table']['group'] = t('Poll');

  $data['poll_choice']['table']['join'] = array(
    'node' => array(
      'left_field' => 'nid', 
      'field' => 'nid',
    ),
  );

  // PK `chid` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique identifier for a poll choice.',
  $data['poll_choice']['chid'] = array(
    'title' => t('Poll choice unique identifier (chid)'), 
    'help' => t('Unique identifier for a poll choice.'),
    'field' => array(
      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  // `nid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'The node.nid this choice belongs to.',
  $data['poll_choice']['nid'] = array(
    'title' => t('Poll choices'), // apparaît dans la liste des relations
    'help' => t('The node.nid this choice belongs to.'),
    );

  // `chtext` varchar(128) NOT NULL DEFAULT '' COMMENT 'The text for this choice.',
  $data['poll_choice']['chtext'] = array(
    'title' => t('Choice label'), 
    'help' => t('The text for this choice.'), 
    'field' => array(
      'handler' => 'views_handler_field', 
      'click sortable' => TRUE, // This is use by the table display plugin.
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ), 
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  // `chvotes` int(11) NOT NULL DEFAULT '0' COMMENT 'The total number of votes this choice has received by all users.',
  $data['poll_choice']['chvotes'] = array(
    'title' => t('Number of votes for the choice'), 
    'help' => t('The total number of votes this choice has received by all users.'), 
    'field' => array(
      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  // `weight` int(11) NOT NULL DEFAULT '0' COMMENT 'The sort order of this choice among all choices for the same node.',
  $data['poll_choice']['weight'] = array(
    'title' => t('Choice weight'), 
    'help' => t('The sort order of this choice among all choices for the same node.'), 
    'field' => array(
      'handler' => 'views_handler_field_numeric', 
      'click sortable' => TRUE,
    ), 
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ), 
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
  
  return $data;
}

It seems to work fine, and I achieved to get the list of polls ordered by number of votes, using agregation.
Perhaps it would be useful for other users to have this implemeted in the views module.

Comments

guypaddock’s picture

Assigned: Unassigned » guypaddock

I'll take this one. I'm working on a patch to add support for all of the tables exposed by core's Poll module.

guypaddock’s picture

Status: Active » Needs review
StatusFileSize
new9.37 KB

The attached patch should address this feature gap.

dawehner’s picture

Status: Needs review » Needs work
+++ b/modules/poll.views.inc	Sun May 20 20:20:31 2012 -0400
@@ -15,41 +13,338 @@
-      'handler' => 'views_handler_field_boolean',
+      'handler'        => 'views_handler_field_boolean',
       'click sortable' => TRUE,

Would it be okay to not change some kind of codestyle? Personally i prefer to not aligned version.
In general please try to have look at http://drupal.org/coding-standards and implement your code based on that.

In general your code looks fine, it's quite a complete integration for these tables, thanks!

+++ b/modules/poll.views.inc	Sun May 20 20:20:31 2012 -0400
@@ -15,41 +13,338 @@
+  $data['poll_choice']['table']['join'] = array(
+    'poll_vote' => array(
+      'left_field'  => 'chid',
+      'field'       => 'chid',
+    ),
+    ¶
+    'node' => array(
+      'left_field'  => 'nid',
+      'field'       => 'nid',

I'm not sure whether there is a real note for a implicit relationship between node and poll_vote here. Wouldn't the relationship be enough for these kind of use cases?

rohr’s picture

Thanks, Guy. You are awesome. I'm surprised this was not available for so long. Appreciate your work.

guypaddock’s picture

Status: Needs work » Needs review
StatusFileSize
new8.53 KB

Updated patch for latest Views code 7.x-3.5.

@dawehner: No need to cite the coding standards; I'm familiar with them :). Problem was just that my editor wasn't cooperating and I wanted to get this patch out there for others during the brief moment I had to post it. This new patch should have better formatting.

As for the implicit relationship, I haven't found a good way to get to the choice text / choice IDs from the poll votes without having an implicit relationship present to the node. Perhaps you can make a suggestion (with code)? The only way that seemed like it would work involved making poll choices a base table, which seems unnecessary.

dawehner’s picture

You can also make a

$data['node']['poll_choice'] = array(
  'relationship' ...
);
sinasalek’s picture

Patch applied cleanly and works like a charm,
Thank's for sharing.
I compared field uid definition in your patch to comment.views.inc, As you can see there are two differences
1. in the following code 'base field' is being use instead of 'uid'
2. 'views_handler_filter_numeric' used instead of 'views_handler_filter_user_name'

I tried both ways and both seem to be working, however i change the code after i created the view. So can't tell for sure if the test is accurate since i don't know wether code changes reflect on already created views or not

   $data['comment']['uid'] = array(
    'title' => t('Author uid'),
    'help' => t('If you need more fields than the uid add the comment: author relationship'),
    'relationship' => array(
      'title' => t('Author'),
      'help' => t("The User ID of the comment's author."),
      'base' => 'users',
      'base field' => 'uid',
      'handler' => 'views_handler_relationship',
      'label' => t('author'),
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    ),
    'field' => array(
      'handler' => 'views_handler_field_user',
    ),
  );
Spokenbird’s picture

Anyone know if this is possible for Advanced Poll?

chris matthews’s picture

Version: 7.x-3.3 » 7.x-3.x-dev
Assigned: guypaddock » Unassigned
Status: Needs review » Needs work
Issue tags: +trim whitespace
views-1506612-full_polls_support-6.patch:15: trailing whitespace.
  
<code>views-1506612-full_polls_support-6.patch:27: trailing whitespace.
  
views-1506612-full_polls_support-6.patch:28: trailing whitespace.
  // Duration of poll
views-1506612-full_polls_support-6.patch:29: trailing whitespace.
  $data['poll']['runtime'] = array(
views-1506612-full_polls_support-6.patch:30: trailing whitespace.
    'title' => t('Poll duration'),

Checking patch modules/poll.views.inc...
Hunk #2 succeeded at 44 (offset -1 lines).

Applied patch modules/poll.views.inc cleanly.
warning: squelched 286 whitespace errors
warning: 291 lines add whitespace errors.