On my D7 site I am trying to create an exposed filter where current users would choose whether he wants to see all nodes, or only those that current users have not commented. Tried adding 'Comment: Comments of the node' and 'Comment: Author' relationships, 'Comment: Author uid' contextual filter. I didn't want to set a default value because I want users to switch between all nodes and ones commented by current user. Then I added a filter criteria 'Comment: Author uid' with setting up grouped filter using [current-user:uid] token. No success yet.
1 Answer
As I understand it, this is impossible with just the views module by itself.
You can easily filter for nodes that the user has commented on, but filtering for nodes the user hasn't commented on is difficult for reasons I'll go into later, so you're going to have to install additional modules or write your own code.
First of all, to get the nodes the user has commented on, you've already added the correct relationships ("Comment: Comments of the node" and "Comment: Author"). However, the filter you actually need to use is "User: Current" with your author relationship selected and the option "Is the logged in user: Yes". You probably also want to use the "Distinct" option in the view's "Query Settings", otherwise you'll get duplicate rows if the user has commented multiple times on the same node.
Now, the reason you can't easily get the nodes the user hasn't commented on is because you can't just change the filter option to "Is the logged in user: No".
Why not?
Because that will go through every comment on the node, and if any of the comments are not authored by the current user, the node will be included in the results, even if it has other comments that were authored by the current user.
So, what you'd actually need to be able to do is take the list of nodes which the current user has commented on, and then exclude them from the results of the view, which simply can't be done in the standard views UI.
Without writing your own module to modify this behaviour, the best option seems to be the OtherView Filter module.
With that module, you can create a second view of nodes, which excludes the results of the first view I described above, using a "Content: Other view result" filter. For usability you'd probably want to set this up as a grouped filter to avoid having the operator and view names exposed to the user.
-
Thanks, it worked fine with a single filter, that I am basically satisfied with since the nodes commented by current-user are not visible. But when I am trying to create a grouped filter with two items ('one of' and 'not one of' + my new view) I am getting following message: 'Warning: explode() expects parameter 2 to be string, array given in other_view_filter_handler_filter_view->query() (line 142 <...>'. Is there any workaround for this? Feb 26, 2020 at 21:34
-
1@TimurKutuev Sounds like you might have run into the bug I reported. I provided a patch there so you might like to give it a try and comment on the issue if it works for you. If you don't want to try the patch, a workaround is to check the "Allow multiple selections" option on the filter, save the view, then uncheck that option and save the view again.– morbiDFeb 27, 2020 at 9:22
-
Fine! Checking/unchecking "Allow multiple selections" worked nicely. Will note your patch as well for future. Feb 27, 2020 at 9:53
-
FYI, the maintainer of the OtherView Filter module has committed my patch to the dev branch (as well as another of my patches which fixes an issue with "Allow multiple selections") so you could use that now instead of the workaround.– morbiDFeb 28, 2020 at 9:21