Problem/Motivation

As far as I can see, there is no method to get all flags for a particular user.

Steps to reproduce

n/a

Proposed resolution

Add a method to \Drupal\flag\FlagService - perhaps call it getFlagsByUser

Remaining tasks

1. add the method
2. use it in \Drupal\flag\FlagService::unflagAllByUser

User interface changes

none

API changes

Add new method on FlagService

Data model changes

none

Issue fork flag-3381654

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

malcomio created an issue. See original summary.

viren18febs’s picture

Assigned: Unassigned » viren18febs

I am working on it.

malcomio’s picture

Assigned: viren18febs » Unassigned
Status: Active » Needs review

@viren18febS thanks but I just created a merge request for this: https://git.drupalcode.org/project/flag/-/merge_requests/49

joelpittet made their first commit to this issue’s fork.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

Thanks I just caught up the code, it was failing on some cleaned up namespace use statements. We are using this and it's very helpful, thank you @malcomio. I manually reviewed the code, and the abstraction is nice clean-up for the unflagAllByUser()

ivnish’s picture

Status: Reviewed & tested by the community » Needs work

ramil g made their first commit to this issue’s fork.

joelpittet’s picture

Status: Needs work » Reviewed & tested by the community

Looks like the issues were corrected, thanks Ramil g. The remaining are unrelated phpstan for other tests, not appropriate to fix here.

ivnish’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

Good work, but needs tests

joelpittet’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs tests

Tests added, back to RTBC.

ivnish’s picture

Looks good for me. Waiting for @Berdir opinion.

ivnish changed the visibility of the branch 8.x-4.x to hidden.

ivnish’s picture

Status: Reviewed & tested by the community » Closed (duplicate)
joelpittet’s picture

Version: 8.x-4.x-dev » 5.x-dev
Status: Closed (duplicate) » Reviewed & tested by the community

@ivnish, I think this one’s actually a different use case, so reopening as RTBC 🙂
The diff:

  • getFlagUserFlaggings($flag, $user): Only returns the user’s flaggings for a specific flag.
  • getAllFlaggingByUser(?$account, $flag = NULL, $session_id = NULL): Returns all flaggings for a user, with extra handling for anonymous users via session ID.

Since getFlagUserFlaggings() already covers the per-flag case, maybe we could simplify getAllFlaggingByUser() to just handle “all by user” and drop the “optionally narrowed by flag” bit? That way each method has a clear, distinct role.

Looks like you’re targeting 5.x, so I’ve moved it over there.

joelpittet changed the visibility of the branch 5.x to hidden.

ivnish’s picture

so reopening as RTBC

Ah, sorry)

Since getFlagUserFlaggings() already covers the per-flag case, maybe we could simplify getAllFlaggingByUser() to just handle “all by user” and drop the “optionally narrowed by flag” bit? That way each method has a clear, distinct role.

Could you update the MR ?

Or... I can revert my commit and use yours, because it more complex

joelpittet’s picture

@ivnish woah, you’re quick! I was juggling branches since I couldn’t retarget the MR to 5.x, so I created a new one and moved the code over.

Totally up to you, but my thought is to keep the signature specific to “all flags for user,” so the distinction between the two methods stays clear.

joelpittet’s picture

Thanks for retargetting the code in MR 49 to 5.x branch. I will put my proposal in MR 156 since they are now both on 5.x

joelpittet’s picture

Status: Reviewed & tested by the community » Needs review

I added populateFlaggerDefaults() because I think we were missing that, and removed the $flag because that use-case is covered by the other issue. Moving to Needs Review, maybe you can have a look @ivnish?

ivnish’s picture

Status: Needs review » Reviewed & tested by the community

LGTM

if no more code updates are planned, I think I can commit this

joelpittet’s picture

@ivnish I am good with committing this as-is. We could test session too but there are probably other tests already checking for that condition as we copied from other service methods. So 🚀

ivnish’s picture

Status: Reviewed & tested by the community » Fixed

Thanks!

joelpittet’s picture

Awesome, thanks for giving the feature a second thought!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.