I have been trying hard but coming up short on a way to do this: I would like to create a Views listing of all the nodes that the current user has *not* commented on. Some of the things I have tried are:

-View of type "node", with Relationship of "Comment - User", and filter set to "Current User - No". Problem is, it displays all nodes that have *any* comments not by the current user.

-View of type "comment" with Relationship to the "Comment - User" and "Comment - Node", field set to the "Node - Title", and filter set to "Current User - No". Problem is, it displays node titles for every comment, not just 1 per node.

Does anyone know the way to do this? If it requires some custom php code, that's fine, but I'm a Drupal beginner and I'm not sure where to put the code.

Side note: I'd also like to do the same thing with voting. I'm using the module "pollfield" in a CCK node, and would like to display all nodes the current user has not voted on. This is probably a different beast, but thought I'd mention it in case anyone has done something similar.

Thanks for any help!

Comments

volongoto’s picture

Hi,

I'm looking for the same thing. Were you able to accomplish this?

Thanks.

narayanis’s picture

what about using the views_query_alter hook? you can create an additional where clause. here's some pseudo-code that should get you started:

function my_custom_views_query_alter(&$view, &$query) {

//only implement for views that match the name
	if($view->name == "myviewname") {
	
$view->query->add_where("", "nid not in (select nid from comments where uid = %d)", $user->uid);

}

there are likely more performant ways to do this if you're making a high-traffic site, but this is the best i can think of.

volongoto’s picture

narayanis, thanks for your reply. Since I'm not very experienced yet in Drupal, could you also mention where to insert this piece of code?

Performace: The block I'll create will be visible only to a few people, so it shouldn't be a big issue for now.

Cheers.

volongoto’s picture

Hi,

Here is the code from my first iteration. It didn't seem to work. The view normally (without this hook) displays all the nodes (which the user has not created herself). And after putting this function into a custom module it doesn't seem to change anything. The only thing I added is the line with global $user. Any ideas?

function my_custom_views_query_alter(&$view, &$query) {
    //only implement for views that match the name
    if($view->name == "my_view_name") {
      global $user;
      $view->query->add_where("", "nid not in (select nid from comments where uid = %d)", $user->uid);
    }
}

Cheers.

narayanis’s picture

As I mentioned, this was example code; it is untested and possibly has a syntax error, but you can use it as a starting point. Try using the drupal_set_message command in your module to print out the views object and ensure the WHERE clause is being added as expected.

solobobo’s picture

Subscribing. Can't find any effective method to solve this problem :(

alirezaara’s picture

Subscribing