In my node View, I would like to display VoteResult (sum) value of each node, and I do not see any field or relationship to support the same.

From API.txt:

VotingAPI provides Views Relationship connections for votes cast on nodes and comments.

However, this doesn’t seem to be the case. Implementing hook_votingapi_relationships (for node table) doesn’t help as well. How do I show the total votes received by each node in my view? My Drupal core is 8.0.5, in case that matters.

Comments

ravi_sam created an issue. See original summary.

ravi_sam’s picture

The attached patch provides VoteResult views data and views relationship for nodes, users and comments views. With this, it is possible to display the total votes cast on these entities, in their respective views. Pls review.

recrit’s picture

Status: Active » Needs review
recrit’s picture

The attached patch takes a more generic approach created views relationships for every content based entity type installed on the system. The patch in #2 provided node, comment, and user relationships and assumed all were installed on the system.

The following relationships are added for each entity type, votingapi result function (average, sum, count), and vote type (up, down, ...):

  1. Entity type & Result Function: One entity to many vote results relationship. Example: 'Vote Result "Sum" for content items'. Usage: This will produce multiple rows for a single node.
    NID Vote type Vote Value
    1 Up 12
    1 Down 6
  2. Entity type & Result Function & Vote Type: Flattened relationship for each vote type. Example: 'Vote Result "Sum" for content items: Like/Up/Yes vote'. Usage: Provide a column for each result sum.
    NID Up Down
    1 12 6
recrit’s picture

Patch update - added back base table definition per patch #2.

malcomio’s picture

Status: Needs review » Needs work

With the patch from #5 applied, I tried to create a "Most popular" view on a site with the current state of core's 8.2.x branch.

I observed two issues:

  1. When adding a relationship, the various vote result options each appeared 3 times
  2. After adding the relationship "Vote Result "Average" for content items", a fatal error occurred when saving the view: Drupal\Core\Config\ConfigValueException: node_vote_result_vote_field_average:node.field_rating key contains a dot which is not supported. in Drupal\Core\Config\ConfigBase->validateKeys()

The second issue seems to be related to #2780869: Cannot save a view where a filter value option contains a period

rbrandon’s picture

I updated the patch from #5, I was able to remove some duplicate code and fixed both issues mentioned in #6.

The fatal error was easy to overcome by not using the period in the views handler key. There were not many duplicates, there is one relationship added for each field type and for each vote type, so the product is a little large and confusing. For the fields I added the field name into the label so that you can understand what relationship you are targeting.

kpv’s picture

Support for entity types other than content would also be great to have.
I'm using a contact form together with Contact storage to collect user reviews. After that those reviews can be rated. Since it's neither a content entity nor translatable, a relationship doesn't appear in Views.
Here is the code from #5 with the check:

diff --git a/votingapi.views.inc b/votingapi.views.inc
new file mode 100644
+  foreach ($entity_types as $entity_type_id => $entity_type) {
...
+    // Limit to content entity types.
+    if ($entity_type->getGroup() == 'content' &&
+        $entity_type->isTranslatable() &&
+        $entity_type->getKey('id')) {
+      $views_entity_types[$entity_type_id] = $entity_type;
+    }
+  }
betz’s picture

Someone made any progress on views integration?

grinder3011’s picture

I just recently had the need of relationships for Voting API and tested the votingapi-views-relationships-for-vote-result-2731101-7.patch which works fine for me.

Therefor to the developers and maintainers of Voting API, I also ask if you can include the patch so that those who come here for the same reason don't need to go through patching needlessly.

malcomio’s picture

Status: Needs work » Needs review

Setting status back to "Needs review" so that testbots and maintainers will see this issue (see https://www.drupal.org/node/156119 for more info on status)

codeyourdream’s picture

We have updated the patch from #7 with the ability to sort the views by voting result. If an entity has no votes, it will be sorted to between entities with positive and negative results (as if it's result is 0).

maaty388’s picture

What about in search_api views I cannot see vote relationships?

maaty388’s picture

I'm getting error on page

Notice: Undefined property: Drupal\search_api\Plugin\views\query\SearchApiQuery::$tables in votingapi_views_query_alter() (line 83 of modules/contrib/votingapi/votingapi.module).
Warning: Invalid argument supplied for foreach() in votingapi_views_query_alter() (line 83 of modules/contrib/votingapi/votingapi.module).
sylus’s picture

StatusFileSize
new7.67 KB

Just adding this patch as a quick workaround so am not getting issues with search api views.

sylus’s picture

StatusFileSize
new7.67 KB

Just adding this patch with a better workaround so am not getting issues with search api views and others.

blazey’s picture

StatusFileSize
new7.49 KB

Attached patch is based on the one from #16. It removes the piece of code mentioned in #8, so the views integration works with contact messages.

blazey’s picture

StatusFileSize
new1.77 KB

The patch from #17 was broken, attaching the proper one.

dunebl’s picture

Maybe I missed something, but I have tried several patches : #7, #16 and #18... none of them could solve my sort problem.

Drupal 8.4-dev
votingapi 8.3.x-dev

I have created a content type (node) with a voting field:

Vote plugin: 5 stars rating
Vote type: Normal vote (default)=>points
Number of allowed values: 1

And a view to list this content type (no relationship).
I have tried several combination of the field: read-only (yes/no), show result (yes/no), Show own vote (yes/no) without success

pifagor’s picture

StatusFileSize
new1.57 KB

Please test. I recreated patch, but I didn't test it.

  • pifagor committed 0167a3c on 8.x-3.x
    Issue #2731101 by recrit, blazey, sylus, ravi_sam, pifagor,...
pifagor’s picture

Status: Needs review » Fixed
alesbencina’s picture

I have added relationship but sorting not really working.

pifagor’s picture

can you write all the dependencies and step-by-step instruction to reproduce the problem?

alesbencina’s picture

I have tested the last patch with a combination of votingapi widgets module (dev version), which populates tables votingapi_results and votingapi_vote correctly. Or maybe should I go through all these patches and apply them 1 by 1?

drupalgin’s picture

Maybe, you have to test on the version 8.x-3.x-dev of the module?

alesbencina’s picture

@drupalgin I test on Voting API 8.x-3.x-dev.

alesbencina’s picture

Status: Fixed » Needs review
StatusFileSize
new5.2 KB

Hello, to apply vote relationships on a view you can use this patch. It's from patch #7.

When adding a relationship you have to select "Vote Result "Average" for content items: Normal vote" or other if you are working with votingapi_widgets module.

If you want to sort ASC or DESC you have to select Vote result: Value and set sort order to top (it have to be first).

Please test it.

labboy0276’s picture

Status: Needs review » Reviewed & tested by the community

The patch in #29 correctly solves the issue of implementing a relationship + view fields. I tested it out and it works as stated.

  • pifagor committed 7b3984a on 8.x-3.x authored by alesbencina
    Issue #2731101 by recrit, sylus, blazey, pifagor, alesbencina, ravi_sam...
pifagor’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

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

dchakrab’s picture

Also tested patch in #29, can verify that it's working. Can we have this rolled back into the module?

markwittens’s picture

I have also installed and tested patch #29 successfully.