Hi - Does anyone know how to replicate the layout of collectiveopinions.com - i.e. embed a Plus1 button as a field in the view, which both displays the number of votes and enables for Plus1 voting to occur?

Thank you!

Comments

newmediaist’s picture

One way I have discovered is by calling the widget from the row template for the view, rather then as a field in the view configuration. In order to implement the plus1 button from the template you must first grab the NID from the row data, then get the node object and pass it to the plus1 widget.

merto87’s picture

Version: 6.x-2.4 » 6.x-2.6
Category: support » feature
Priority: Normal » Critical

Can you post example code?

I've created a views-view-field--MYFIELD--value.tpl.php template.

In this file there's:

<?php 
print(plus1_jquery_widget($row->nid, $teaser, $page));
?>

The widget is printed, but:
- widget score is same for all fields;
- Vote button doesn't work: the javascript require this path: /plus1/vote/?token=94f8e17c56f5f059d5ea1a0896dce81f&json=true without node_id after arguments.

Thank you very much and sorry for my english!

GMercey’s picture

I have the same problem! Anybody have an idea ?

voxpelli’s picture

Priority: Critical » Normal

This is certainly not a critical issue - while nice to have it is nothing that stops Plus1 from working.

My suggestion would be to use teasers of some form if possible

ximo’s picture

Title: Create view with Plus1 button embeded, similar to www.collectiveopinions.com/? » Plus1 widget as a field in Views

I've written a small Views field handler for this. It works, but it's not really recommended from a performance perspective. Each plus1_jquery_widget() call needs a node object, and a node_load() call is pretty expensive in a listing.

I could avoid doing a node_load() by passing a fake node object with only the data needed, and I might do just that. If I do, I'll be back with a patch. If not, you're best of doing what voxpelli says, presenting the nodes as teasers and theming that to your needs :)

(Rewrote the title...)

jackiepeters’s picture

Same problem here, argh!

marcoka’s picture

using views with plus1 has crumbled my concept :). i have that problem now, too

dakotaryan’s picture

Ran into this same issue, and am not sure how to fix within the module itself. However, I found a hacky solution using Views Custom Field, which will let you customize the results of a views query.

Specifically, I used the following code in the PHP code customfield:

global $user;
$user_likes = intval(plus1_get_votes($data->nid, $user->uid));
$other_likes = intval(plus1_get_score($data->nid) - $user_likes);

print '<div class="plus1-widget"><div class="plus1-msg"><div class="plus1-vote">'.plus1_custom_msg($data->nid, $user_likes, $other_likes).'</div></div><div class="plus1-score">'.max($user_likes + $other_likes, 0).'</div></div>';

And the following code I added into my theme's template.php:

function plus1_custom_msg($nid, $user_likes = 0, $other_likes = 0) {
	if ($user_likes) {
		$msg = '<div class="plus1-link">';
		if ($other_likes == 0) 		$msg .= "You liked this";
		else if ($other_likes == 1)	$msg .= "You and 1 other reader liked this";
		else if ($other_likes >= 2)	$msg .= "You and $other_likes other readers liked this";
		$msg .= "</div>";
	} else {
		$msg = '<a href="/plus1/vote/'.$nid.'?token='.drupal_get_token($nid).'" class="plus1-link">I like this</a> ';
		if ($other_likes == 1)	$msg .= "(1 other reader did too)";
		else if ($other_likes >= 2)	$msg .= "($other_likes other readers did too)";
	}
	return $msg;
}

I also commented out the following line the jquery.plus1.js (since I'm handling it differently):
//plus1_widget.find('.'+ Drupal.settings.plus1.score_class).hide().fadeIn('slow').html(json.score);
and in order to have the context-based messages appear when the JSON loads, you'd have to play around some more with the JS. Not my goal.

Probably not the most efficient or elegant solution, but it works and lets me add a live voting widget into a views field, kind of. Hope this will be a viable solution until something cleaner is implemented into the plus1 module. By the way, I'd also love to see context-based messages (aka. 3 other users liked this) supported and customizable out of the box.

Cheers, Dakota

joeshock’s picture

@dakotaryan

Great post and ideas. I just implemented your suggestions and they work great. One thing I added however was the ability for you to go back to the page from which you voted with a simple addition of &destination=$_SERVER["REQUEST_URI"] at the end of the vote link url.

So all I updated was the code that goes into the template.php file, which is below:

<?php

function plus1_custom_msg($nid, $user_likes = 0, $other_likes = 0) {
    if ($user_likes) {
        $msg = '<div class="plus1-link">';
        if ($other_likes == 0)         $msg .= "You liked this";
        else if ($other_likes == 1)    $msg .= "You and 1 other reader liked this";
        else if ($other_likes >= 2)    $msg .= "You and $other_likes other readers liked this";
        $msg .= "</div>";
    } else {
        $msg = '<a href="/plus1/vote/'.$nid.'?token='.drupal_get_token($nid).'&destination='.$_SERVER["REQUEST_URI"].'" class="plus1-link">I like this</a> ';
        if ($other_likes == 1)    $msg .= "(1 other reader did too)";
        else if ($other_likes >= 2)    $msg .= "($other_likes other readers did too)";
    }
    return $msg;
}
?>
joeshock’s picture

@dakotaryan

here's another idea to consider. try this one out if you want. it's what might work for me in the site i'm building.

<?php

function plus1_custom_msg($nid, $user_likes = 0, $other_likes = 0) {

		//link start
		if($user_likes == 0) { $msg .= '<a title="Like this" href="/plus1/vote/'.$nid.'?token='.drupal_get_token($nid).'&destination='.substr($_SERVER["REQUEST_URI"],1).'" class="plus1-link">'; }
		
		// 0 Likes - 1 Like - x Likes
		if($user_likes + $other_likes < 1) {$msg .= '0';}
		if($other_likes + $user_likes == 1) {$msg .= '1';}
		if($other_likes + $user_likes > 1) {$msg .= $other_likes + $user_likes;}
		
		$msg .= ' Like';
		
		if($user_likes + $other_likes > 1)	{$msg .= 's';}
		if($user_likes + $other_likes == 1)	{$msg .= '';}
		if($user_likes + $other_likes < 1)	{$msg .= 's';}
		
		//link end
		if($user_likes == 0) { $msg .= '</a>'; }
    return $msg;
}

?>
joeshock’s picture

Saw this other module that might be better as an option. It's called Drupal It. Works out pretty well, however there is one downside I've encountered and it seems that you have to be an authenticated user to vote. Sticking with the Plus1 setup, but just throwin this one out there too.

http://drupal.org/project/drupalit

Only thing you have to figure out by Googling a little bit is how to "manually" place this system within a view. Just place this anywhere within your view after configuring the DrupalIt settings to "manual" and you'll be all set.

<?php

echo drupalit_drupalit_node($node->nid);

?>
sebsebseb123’s picture

Hi All.

I'm using a views tpl.php file to customize my views output. I wanted to manually embed the plus1 widget, but I had the same problem you're all having....same score, unable to vote...etc... I looked into the plus1_jquery_widget() function... Turns out the first parameter is NOT a node_id value. But rather, the entire node.

So, going back to comment #2, where the nid is sent:

print(plus1_jquery_widget($row->nid, $teaser, $page));

It should be this instead:

$node = node_load($row->nid);
print(plus1_jquery_widget($node, $teaser, $page));
eelkeboezeman’s picture

this last suggestions worked brilliantly. thanks!

joeshock’s picture

Issue tags: +vote, +ip address

worked for me too

joeshock’s picture

Issue tags: -vote, -ip address
thelionkingraja’s picture

Title: Plus1 widget as a field in Views » please send me the worked solution
Component: Code » User interface
Assigned: Unassigned » thelionkingraja
Category: feature » support

please send me the worked solution with step by step process.

webthingee’s picture

My quick and easy consisted of. combining...

#8 - "I found a hacky solution using Views Custom Field, which will let you customize the results of a views query."

- - installed Views Custom Field.
- - added a php field.

#12 - used this code, but changed $row to $data.

$node = node_load($data->nid);
print(plus1_jquery_widget($node, $teaser, $page));

so far so good...

nancydru’s picture

Title: please send me the worked solution » Embed Plus1 button as field in a view
Status: Active » Postponed (maintainer needs more info)

Is there a patch or anything here?

Weka’s picture

subscribe

drupalninja99’s picture

In my view I am using a views custom php field with:

return module_invoke('plus1', 'jquery_widget', $data, 0, 1);

And that works pretty good, it seems like plus1 could expose that very thing itself. The only issue is that in plus1 1x it did ajax, now it does a redirect.

nancydru’s picture

It was changed from Ajax because of #322735: Vote link is executed by search engines. I don't understand Views coding very well, so someone will have to submit a patch and have it RTBC'ed.

justinlevi’s picture

I was able to get this to work in my D7 views-view-fields--VIEWNAME.tpl.php file as follows:

  $widget = module_invoke('plus1', 'build_node_jquery_widget', $row->nid, 0, 1);
  print render($widget);
nancydru’s picture

Thanks.

WhatUp’s picture

Version: 6.x-2.6 » 7.x-1.x-dev

Hi!

Could someone please explain how to do this in Drupal 7? The Views Custom Field module only exists for D6.

A up to date description would be great!

Regards.

federico’s picture

Hi, I'm trying to do this in D7, could someone help?

My view is called "viewname". I create a new file on /sites/all/themes/mytheme/templates called "views-view-fields-viewname.tpl.php" and pasted this code in it:

<?php 
$widget = module_invoke('plus1', 'build_node_jquery_widget', $row->nid, 0, 1);
 print render($widget);

Should I use delimiters?

- I go to /admin/structure/views/view/links/edit
- I Click on fields > add and search for something like "widget", but I don't find anything.

I copy the file views-view-fields-viewname.tpl.php to /sites/all/modules/views/theme and repeat the procedure, but I still can't find the new field.

I'll appreciate any help. Thanks in advance.

exlin’s picture

Proper views integration (d7-3.x) would be great.
Including field for sorting and fields.

Maybe preferred way in d7 is to use fields for this...

AndyThornton’s picture

drupal's comment need to implement "+1". I give a +1 to #20 ... works perfectly and seems like the right and most elegant solution.

shashi_lo’s picture

#17 Was exactly what I was looking for. Thanks!

FR6’s picture

Issue summary: View changes

Not sure if it's the best way to do but what I did to display the Plus 1 widget in a view is:

In my file views-view-fields--MyView.tpl.php:

echo render(plus1_build_node_jquery_widget($nid));

I'm using Drupal 7.31 with plus1-7.x-1.0-alpha2.