6

I'd like to create a view for the user's profile page that displays all unread comments on the nodes he/she created. When I say 'unread', I mean that the page where the comment is situated (namely, the node's page) hasn't been visited by the user yet. In other words: when the user hasn't read a new comment, it will be displayed in the view; once the user has read it, it will disappear in the view.

I've already set up a view that simply displays all comments on the user's nodes, but I can't find an extra 'Filter criteria' or 'Relationship' that filters out the unread ones.

Can you help me?


Edit: I created a flag called 'Read' ('Reactie gelezen' in Dutch) with the Flag module and a rule 'Comment read' that automatically flags comments with 'Read' when they're viewed. I've also granted every user the permission to flag comments with the 'Read' flag (although they won't see the flag links above their comment of course).

My rule works perfectly and the flagging works manually too. The view however, doesn't work properly. These are my settings:

Settings view

Both uid's were provided the default value 'User ID from URL'.

What did I do wrong?

5
  • 1
    This may sound stupid, but what is your criteria for a comment being unread? What will trigger a comment as being marked as read?
    – mpdonadio
    Oct 10, 2013 at 19:37
  • When I say 'unread', I mean that the page where the comment is situated (namely, the node's page) hasn't been visited by the user yet.
    – Jeroen
    Oct 10, 2013 at 19:57
  • Hi, What is the view output ? And how the "flag" works in this scenario, as well.. ? Is it ajax-jquery or ? Nov 2, 2013 at 13:37
  • Also, can you try displaying all the unread comments, without the user filter.... Nov 2, 2013 at 13:39
  • @OlegVidenov: I discribed the view's output on my question: it contains all the unread comments on the user's nodes. I added a link to my exported 'Read' flag. User don't see this flag, it's just a workaround for the view. I really have the intention to show the unread comments with the user filter.
    – Jeroen
    Nov 2, 2013 at 17:47

1 Answer 1

2
+50

The Flag module only keeps rows in the database for content where a flag is set. I.e. when something is not flagged, there will be no corresponding row in the database.

The problem is, that views will generate a query which looks something like this (for a user with uid=12 and a flag with fid=1):

SELECT cid FROM comment AS c
INNER JOIN node AS n ON c.nid = n.nid
LEFT JOIN flagging AS f ON c.cid = f.entity_id AND f.fid = '1'
WHERE n.uid = '12' AND f.uid = '12'

However in order to select all comments for a given user excluding those which are not flagged, you need an SQL subquery:

SELECT cid FROM comment AS c
INNER JOIN node AS n ON c.nid = n.nid
WHERE n.uid = '12' AND c.cid NOT IN (
    SELECT f.entity_id FROM flagging AS f
    WHERE f.fid = '1' AND f.uid = '12'
)

AFAIK Views has no built-in support for subqueries. I therefore recommend you to reverse the logic and implement the following:

  • Add a comment-flag is_new
  • Whenever a new comment is posted, execute a rule which sets this flag on behalf of the content author.
  • Whenever a node is viewed by its author, execute a rule which removes all is_new-flags on the associated comments (this is essentially the same rule you already have, except that you need to reverse the flagging-logic).
  • Configure the view just as you did before but remove the flagged=false filter, instead configure the flag-relationship such that only flagged content is included.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.