Hello, please help as a 4-module version (drupal 8), getting the number of items a user has flagged?

Comments

RomanovVlad created an issue. See original summary.

TheDucksLover’s picture

This is what I used to do what you wanna do :

$result = db_select('flagging', 'f')
        ->fields('f', array('flag_id'))
        ->condition('flag_id', $flag_id)
        ->condition('uid', $user_id)
        ->countQuery()
        ->execute()
        ->fetchField();

where $flag_id is the machine name of your flag (mine was "bookmarks" so it's a string) and $user_id is the uid of the user... I'd advise you to put this function in your theme.theme file or some generic file so u could use it whereever u want.

joachim’s picture

Issue tags: -API

Does the FlagCount service have what you need?

(BTW: please don't add random tags to issues!)

RomanovVlad’s picture

"$result = db_select('flagging', 'f')
->fields('f', array('flag_id'))
->condition('flag_id', $flag_id)
->condition('uid', $user_id)
->countQuery()
->execute()
->fetchField();"
I need the output node number of flags in the announcement.
This code should be put in node.html.twig, then what to put in place
"$user_id"?

joachim’s picture

Use the FlagCount service.

RomanovVlad’s picture

Use the FlagCount service.
And how to use FlagCount service? This is a separate module?
Or module setting the flag? (Module version 4.X dev 2016-02-19)

socketwench’s picture

The above query is identical to the one we have in FlagCountManager:

<?php
  public function getUserFlagFlaggingCount(FlagInterface $flag, AccountInterface $user) {
    $flag_id = $flag->id();
    $uid = $user->id();

    // We check to see if the flag count is already in the cache,
    // if it's not, run the query.
    if (!isset($this->userFlagCounts[$flag_id][$uid])) {
      $result = $this->connection->select('flagging', 'f')
        ->fields('f', ['flag_id'])
        ->condition('flag_id', $flag_id)
        ->condition('uid', $uid)
        ->countQuery()
        ->execute()
        ->fetchField();
      $this->userFlagCounts[$flag_id][$uid] = $result;
    }

    return $this->userFlagCounts[$flag_id][$uid];
  }
?>

Instead of duplicating the query, you could do this instead:

<?php
if (\Drupal::moduleHandler()->moduleExists('flag')) {
  $flag_count_service = \Drupal::service('flag.counts')->getUserFlagFlaggingCount($the_flag, $the_user);
}
?>

Where $the_flag and $the_user are the flag and user objects respectively.

joachim’s picture

> And how to use FlagCount service? This is a separate module?

Have you actually searched the Flag codebase for 'FlagCount'?
Have you googled to find out about how to use Drupal services?

socketwench’s picture

You can call the count service even in your module, Drupal doesn't care. Furthermore, the FlagCountManager caches counts so you don't always have to hit the database with a query.

mfrosch’s picture

@joachim you can check out the flag.services.yml file for getting informations about the services which the module provides.

Here an extended example from @socketwench with flag load

$flag = \Drupal::service('flag')->getFlagById('following');
$user = \Drupal::currentUser();
$flagcount = \Drupal::service('flag.count')->getUserFlagFlaggingCount($flag, $user);
RomanovVlad’s picture

Thanks for the help.
How to get the number of flags in the node?

socketwench’s picture

As in the number of flags defined administratively, or the number of times a node has been flagged?

socketwench’s picture

There's a getFlags() method on the FlagService.

<?php
$flag_service = \Drupal::service('flag');
$flags = $flag_service->getFlags('node', 'your_content_type');
$number_of_flags_defined = count($flags);
?>
RomanovVlad’s picture

How to use getFlagEntityCount function if content_type="mesto" and flag_id="ponravilos"
What code do I need to insert into html.node.twig
Sorry for my poor English skills and php.
Thank you all for the help

kla2t’s picture

@mfrosch: Employing the flag.count service this way resulted in fatal errors for anonymous users in my case.
I had to get and add a session_id to make it work:

<?php
$flag = \Drupal::service('flag')->getFlagById('my_flag_id');
$user = \Drupal::currentUser();
$session = \Drupal::request()->getSession();
$session_id = $session->getId();
$flagcount = \Drupal::service('flag.count')->getUserFlagFlaggingCount($flag, $user, $session_id);
?>
kitzinger’s picture

@kla2t I'm also looking at this, but isn't getSession now deprecated ?

kla2t’s picture

@kitzinger ...don't think so. It seems to be valid up to 8.7.

ivnish’s picture

Status: Active » Closed (outdated)

5 years without any activity. I think we can close it as outdated. Please reopen if needed.