So, I've been able to:

* Get a list of all (nodes of type A) that the (contextual User ID) has flagged.

This is great for something like user/%/my-flags. Fine. Awesome.

But, what if I wanted to get a little crazier? Say I have "Flag Follower" enabled. I've followed 5 users. Now, I don't want to see their CONTENT (as the default included View does), but I want to see what THEY'VE FLAGGED. And, for bonus points, I'd like to see what WE'VE flagged. To some degree, I'm looking for a single activity stream of all the flags of myself and whom I'm following. Is that possible? Something like https://www.truetrophies.com/gamer/MorbusIff where all of the "user has won this trophy" are the equivalent of "my followed user, or myself, has flagged this content".

Comments

Morbus Iff created an issue.

joachim’s picture

Seems doable with an extra relationship.

Raphael Apard’s picture

Is there a getUserFlaggings() method ? I didn't find it. Only the getUserFlagFlaggingCount() from FlagCountService ?

I had to use a custom query

$flag = \Drupal::service('flag')->getFlagById('flag_id');
$user = \Drupal::currentUser();
$session_id = \Drupal::service('session_manager')->getId();

// Use a query.
$query = \Drupal::database()
        ->select('flagging', 'f')
        ->fields('f', array())
        ->condition('flag_id', $flag->id())
        ->condition('uid', $user->id());

if ($user->isAnonymous()) {
    $query->condition('session_id', $session_id);
}
      
$result = $query->execute()
        ->fetchAll();
droprocker’s picture

But, what if I wanted to get a little crazier? Say I have "Flag Follower" enabled. I've followed 5 users. Now, I don't want to see their CONTENT (as the default included View does), but I want to see what THEY'VE FLAGGED.

I'm searching for the same thing. Does anyone have a hint how to achieve this!?

Joachim, you say it's doable with an extra relationship. Which one?

joachim’s picture

Well you want:

Content -> node related to author user -> user related to flagging -> flagging owned by current user

droprocker’s picture

Well, I'm sorry but I don't get it... I always just get as a result the content the user I follow created/authored, not the content this user has flagged.

joachim’s picture

Can you try debugging the Views query?

droprocker’s picture

The SQL Query is the following:

SELECT node_field_data.created AS node_field_data_created, node_field_data.nid AS nid, node_field_data.langcode AS node_field_data_langcode, flagging_node_field_data.id AS flagging_node_field_data_id, users_field_data_node_field_data.uid AS users_field_data_node_field_data_uid, flagging_users_field_data.id AS flagging_users_field_data_id, 'flag_test_5:page_1' AS view_name
FROM
{node_field_data} node_field_data
INNER JOIN {flagging} flagging_node_field_data ON node_field_data.nid = flagging_node_field_data.entity_id AND flagging_node_field_data.flag_id = 'interested'
LEFT JOIN {users_field_data} users_field_data_node_field_data ON node_field_data.uid = users_field_data_node_field_data.uid
INNER JOIN {flagging} flagging_users_field_data ON users_field_data_node_field_data.uid = flagging_users_field_data.entity_id AND (flagging_users_field_data.flag_id = 'following' AND flagging_users_field_data.uid = '1')
WHERE node_field_data.status = '1'
ORDER BY node_field_data_created DESC
LIMIT 30 OFFSET 0
joachim’s picture

> LEFT JOIN {users_field_data} users_field_data_node_field_data ON node_field_data.uid = users_field_data_node_field_data.uid
INNER JOIN {flagging} flagging_users_field_data ON users_field_data_node_field_data.uid = flagging_users_field_data.entity_id AND (flagging_users_field_data.flag_id = 'following' AND flagging_users_field_data.uid = '1')

Here you're saying the the node author needs to be flagged by the current user.

Web-Beest’s picture

If you want to use the session_id (in case of anonymous users), you should use the session id generated by the flag service in stead of the sessionManager. They are different ;-)

    $flag = $this->flagService->getFlagById('flag_id');
    $user = $this->currentUser;
    $session_id = $this->flagService->getAnonymousSessionId();

    $query = $this->database
      ->select('flagging', 'f')
      ->fields('f', [])
      ->condition('flag_id', $flag->id())
      ->condition('uid', $user->id());

    if ($user->isAnonymous()) {
      $query->condition('session_id', $session_id);
    }
    $result = $query->execute()->fetchAll();