Hi!

Is it possible to allow only 1 flag per user on the comments of the node they own?

As it is now on my site, the node author can flag all comments, but, I want that user to flag only 1 comment. Once 1 comment is flagged, the flag links on the other comments should disappear.

Thanks for any advice!
Tommy

Comments

joachim’s picture

It's doable, in that Flag exposes hooks allowing custom code to check and decide whether to allow a flagging to happen.

You'd need to write custom code to do this though.

tds2012’s picture

Thanks for your reply. Never wrote custom code before, just customized existing code/modules. Where and how should I begin?

joachim’s picture

Gosh. Welcome to coding :)

You'll need to start off by reading up about how to write custom modules and how to use hooks. Lots of tutorials around.

Then, read up on Flag hooks: they are documented in the module's api.php file. You want IIRC hook_flag_validate(), which is probably only on 3.x.

Then implement that, and start experimenting :)

joachim’s picture

Version: 7.x-2.0 » 7.x-3.x-dev
Status: Active » Postponed (maintainer needs more info)

Can you try this with the latest 3.x code please?

The code to convert flag permissions to user permissions has a few awkward things to work around, but I'm tried an upgrade several times and it's worked fine for me.

tds2012’s picture

Version: 7.x-3.x-dev » 7.x-3.0-alpha4
Status: Postponed (maintainer needs more info) » Active

I tried with 7.x-3.x-dev, but when I select the existing flag on the comment, it throws a PDOexception though permissions and so on are correct I think:

PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away: SELECT r.rid AS rid, r.name AS name FROM {role} r INNER JOIN {role_permission} p ON r.rid = p.rid WHERE (p.permission = :db_condition_placeholder_0) ORDER BY weight ASC, name ASC; Array ( [:db_condition_placeholder_0] => flag winner ) in user_roles() (line 2829 of /home/tommy/public_html/xxxxxxx/modules/user/user.module).

when trying to make a new flag on comments, it gives the same error (or similar pdoexc.)

Now installed 7.x-3.alpha4 and the error notice is gone. But ...

how can I now limit flagging to only 1 flag? I don't see any option. Except ..;
unfortunately i only see hook_flag_validate in the dev-release.

function hook_flag_validate($action, $flag, $entity_id, $account, $skip_permission_check, $flagging) {
  // We're only operating on the "test" flag, and users may always unflag.
  if ($flag->name == 'test' && $action == 'flag') {
    // Get all flags set by the current user.
    $flags = flag_get_user_flags('node', NULL, $account->uid, $sid = NULL, $reset = FALSE);
    // Check if this user has any flags of this type set.
    if (isset($flags['test'])) {
      $count = count($flags[$flag->name]);
      if ($count >= 2) {
        // Users may flag only 2 nodes with this flag.
        return(array('access-denied' => t('You may only flag 2 nodes with the test flag.')));
      }
    }
  }
}

suppose I can use this hook (can I just copy it from the flag.api of alpha4 to the flag.api of the dev-release?) and setting the count to 1. Is that allowing only 1 flag on that specific node or only 1 flag on any node throughout the site?

tds2012’s picture

any more advice?

regards
tommy

joachim’s picture

> the flag.api of alpha4 to the flag.api of the dev-release

api.php files are never loaded or executed, they are documentation, as should be stated at the top of it.

> unfortunately i only see hook_flag_validate in the dev-release.

I should make a new release soon... but I am waiting on a couple of blockers.

    // Get all flags set by the current user.
    $flags = flag_get_user_flags('node', NULL, $account->uid, $sid = NULL, $reset = FALSE);
    // Check if this user has any flags of this type set.
    if (isset($flags['test'])) {
      $count = count($flags[$flag->name]);

That's checking flags on all nodes -- read the comments!

anybody’s picture

Issue summary: View changes

This issue is still open. Does someone know if there is a module for this yet or a general solution?

What we need is:
- Hide other flag buttons if there is one comment flagged per entity
- Validation

Thank you very much!

joachim’s picture

Status: Active » Fixed

> I want that user to flag only 1 comment.

It actually sounds like this is an 'accepted answer' type of functionality. There's a module for that!

anybody’s picture

@joachim: Thanks a lot. Could you please provide the link to the module to help me and others with this problem?

joachim’s picture

I don't remember the name. Google it! :)

anybody’s picture

You can be sure I did that before...

flag_limiter.module is the only one I found that might nearly match the "Accept answer" functionality.
https://www.drupal.org/project/flag_solved is good but is not compatible with the latest Flag module.

If you say there's a module for that please always link the module in the future if possible, otherwise that answer isn't helpful for the community, I think. Thank you anyway.

joachim’s picture

anybody’s picture

Thank you very much. That's really nice.
I'll also keep this issue updated if I find another solution that's based on flags.

anybody’s picture

My final results:
https://www.drupal.org/project/flag_solved is the best Flag based solution to mark a single comment as accepted / best answer.
Sadly the module is not made for Flag 7.x-3.x branch.

I created an issue #2471807: Update to new flags version ("Some flags provided by modules need to be updated to a new format before they can be used with this version of Flag. See the disabled flags for a list of flags that need updating.") to discuss a new version for Flags 3 and am working on a new version with the required changes. It works great and so this can finally be called FIXED. :)

best_answer from #3 is a good alternative if you want a complete feature and not a Flags functionality.

joachim’s picture

I've just taken a quick look at the code of Flag Solved module, to take note of any particular hooks it uses that we should make sure Flag D8 keeps.

... urgh. It has major problems. It has a hook_init() that saves an action ON EVERY PAGE LOAD. And it uses the theme preprocessor to prevent a flag from showing, rather than acting on hook_flag_access().

anybody’s picture

@joachim: You are right. I'm currently working on this. I've created a local 7.x-3.x branch of it. If you're interested, let's discuss improvements in the modules issues.

I'm also not sure if actions are the best way to do things there. For my specific case I used rules. Flags 3 also provides some more access settings... all in all the Flags 3 version could be much smarter!

Status: Fixed » Closed (fixed)

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