3

I see extensive Drupal 7 documentation for the Flags module, but very little for Drupal 8. I have a Signup flag on a Drupal 8 node (which is an event). I need to suppress the Signup link when a number of signups (flags) have been added to this node.

I'm trying with hook_preprocess_flag() using the following code.

$flag_service = \Drupal::service('flag');
$flag = $flag_service->getFlagById('signup');

What call gets the flag_count of that flag?

2
  • $flag_service = Drupal::service('flag.count'); $counts = $flag_service->getEntityFlagCounts($node);
    – DiDebru
    Mar 27, 2019 at 13:54
  • Or if you want it for flag entity use $flag_service ->getFlagFlaggingCount($flag);
    – DiDebru
    Mar 27, 2019 at 14:02

1 Answer 1

4

For flag count on entities use:

$flag_service = Drupal::service('flag.count');
$counts = $flag_service->getEntityFlagCounts($node);

For flag count on flag entity use:

$flag_service->getFlagFlaggingCount($flag);

You can find all functions from the Service here:

/modules/contrib/flag/src/FlagCountManager.php

3
  • 1
    Thank you @Insasse, this solved my problem!
    – MEI
    Mar 27, 2019 at 19:06
  • Hi, if I use the above code : $flag_service = \Drupal::service('flag.count'); $flag_count = $flag_service->getFlagFlaggingCount('flag_machine_name'); I get this error : TypeError : Argument 1 passed to Drupal\flag\FlagCountManager::getFlagFlaggingCount() must implement interface Drupal\flag\FlagInterface, string given,.... How Can I get the number of flagged nodes for a flag type? Thanks.
    – user28220
    Dec 6, 2019 at 8:45
  • $flag = \Drupal::service('flag')->getFlagById('flag_machine_name'); Mar 18, 2022 at 19:06

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.