You are here

public function FlagCountManager::incrementFlagCounts in Flag 8.4

Increments count of flagged entities.

Parameters

\Drupal\flag\Event\FlaggingEvent $event: The flagging event.

File

src/FlagCountManager.php, line 194

Class

FlagCountManager
Class FlagCountManager.

Namespace

Drupal\flag

Code

public function incrementFlagCounts(FlaggingEvent $event) {
  $flagging = $event
    ->getFlagging();
  $flag = $flagging
    ->getFlag();
  $entity = $flagging
    ->getFlaggable();
  $this->connection
    ->merge('flag_counts')
    ->key([
    'flag_id' => $flag
      ->id(),
    'entity_id' => $entity
      ->id(),
    'entity_type' => $entity
      ->getEntityTypeId(),
  ])
    ->fields([
    'last_updated' => $this->dateTime
      ->getRequestTime(),
    'count' => 1,
  ])
    ->expression('count', 'count + :inc', [
    ':inc' => 1,
  ])
    ->execute();
  $this
    ->resetLoadedCounts($entity, $flag);
}