You are here

public function FlagService::getAllEntityFlaggings in Flag 8.4

Get all flaggings for the given entity, and optionally, user.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The flaggable entity.

\Drupal\Core\Session\AccountInterface $account: (optional) The account of the flagging user. If NULL, flaggings for any user will be returned.

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

Return value

array An array of flaggings.

Throws

\LogicException An exception is thrown if the given $account is anonymous, but no $session_id is given.

Overrides FlagServiceInterface::getAllEntityFlaggings

File

src/FlagService.php, line 164

Class

FlagService
Flag service.

Namespace

Drupal\flag

Code

public function getAllEntityFlaggings(EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL) {
  $query = $this->entityTypeManager
    ->getStorage('flagging')
    ->getQuery();
  if (!empty($account)) {

    // Use an OR condition group to check that either the account flagged
    // the entity, or the flag itself is a global flag.
    $global_or_user = $query
      ->orConditionGroup()
      ->condition('global', 1)
      ->condition('uid', $account
      ->id());
    $query
      ->condition($global_or_user);
    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);
    }
  }
  $query
    ->condition('entity_type', $entity
    ->getEntityTypeId())
    ->condition('entity_id', $entity
    ->id());
  $ids = $query
    ->execute();
  return $this
    ->getFlaggingsByIds($ids);
}