1

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?

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

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

| improve this answer | |
  • 1
    Thank you @Insasse, this solved my problem! – MEI Mar 27 '19 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 '19 at 8:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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