How to use the flag counter in a twig template of a node? The version dev of the module "Flag" for "Drupal 8" has added a new sub module "Flag Count":

https://www.drupal.org/project/flag/issues/2708291

I copied the code

{% set count = '[' ~ flagcount(flag, flaggable) ~ ']' %}


in flag.html.twig and I copy this file into the templates folder of my theme (I did not cache it).

But after I do not know how to do it. Currently to display the flag link in node.html.twig I put the following code :

{{ content.flag_like_node }}


How to add the number of flag ?

I tested the following code and none works

{{ content.flag_like_node_count }}
{{ content.flag_like_node.count }}
{{ count }}
This will return an array of all the flag counts on a node:

\Drupal::service('flag.count')-&gt;getEntityFlagCounts($node)


(For all flags set for that node)
Via: https://www.drupal.org/node/2476349

You can use that in a preprocess function to get your flag counts and send it as a new variable to your template file. Like so:

function yourtheme_preprocess_node(&amp;$variables) {

  $flag_counts = \Drupal::service('flag.count')-&gt;getEntityFlagCounts($node);
  $variables['like_count'] = $flag_counts['like'];

}


Then in your theme file you can output:

{{ like_count }} 


The problem with this is that it doesn't ajax update when someone clicks the flag link. For that to work you have to put the count in the flag template.