You are here

public function FlagService::unflagAllByUser in Flag 8.4

Remove all of a user's flaggings.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user object.

string $session_id: (optional) The session ID. This must be specified if $account is the anonymous user.

Throws

\LogicException Thrown when $account is anonymous but no associated session ID is specified.

Overrides FlagServiceInterface::unflagAllByUser

1 call to FlagService::unflagAllByUser()
FlagService::userFlagRemoval in src/FlagService.php
Shared helper for user account cancellation or deletion.

File

src/FlagService.php, line 333

Class

FlagService
Flag service.

Namespace

Drupal\flag

Code

public function unflagAllByUser(AccountInterface $account, $session_id = NULL) {
  $query = $this->entityTypeManager
    ->getStorage('flagging')
    ->getQuery();
  $query
    ->condition('uid', $account
    ->id());
  if ($account
    ->isAnonymous()) {
    if (empty($session_id)) {
      throw new \LogicException('An anonymous user must be identified by session ID.');
    }
    $query
      ->condition('session_id', $session_id);
  }
  $ids = $query
    ->execute();
  $flaggings = $this
    ->getFlaggingsByIds($ids);
  $this->entityTypeManager
    ->getStorage('flagging')
    ->delete($flaggings);
}