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.
| Comment | File | Size | Author |
|---|
Comments
Comment #2
guypaddock commentedI'll take this one. I'm working on a patch to add support for all of the tables exposed by core's Poll module.
Comment #3
guypaddock commentedThe attached patch should address this feature gap.
Comment #4
dawehnerWould 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!
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?
Comment #5
rohr commentedThanks, Guy. You are awesome. I'm surprised this was not available for so long. Appreciate your work.
Comment #6
guypaddock commentedUpdated 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.
Comment #7
dawehnerYou can also make a
Comment #8
sinasalek commentedPatch 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
Comment #9
Spokenbird commentedAnyone know if this is possible for Advanced Poll?
Comment #10
chris matthews commented