You are here

public function FlagService::getFlagging in Flag 8.4

Get a single flagging for given a flag and entity.

Use this method to check if a given entity is flagged or not.

For example, to get a bookmark flagging for a node:

$flag = \Drupal::service('flag')
  ->getFlagById('bookmark');
$node = Node::load($node_id);
$flagging = \Drupal::service('flag')
  ->getFlagging($flag, $node);

Parameters

\Drupal\flag\FlagInterface $flag: The flag.

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

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

string $session_id: (optional) The session ID. If omitted and the current user is anonymous the current session id will be used to uniquely identify the anonymous user.

Return value

\Drupal\flag\FlaggingInterface|null The flagging or NULL if the flagging is not found.

Throws

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

Overrides FlagServiceInterface::getFlagging

See also

\Drupal\flag\FlagServiceInterface::getFlaggings()

1 call to FlagService::getFlagging()
FlagService::unflag in src/FlagService.php
Unflags the given entity for the given flag.

File

src/FlagService.php, line 84

Class

FlagService
Flag service.

Namespace

Drupal\flag

Code

public function getFlagging(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL) {
  $this
    ->populateFlaggerDefaults($account, $session_id);
  $flaggings = $this
    ->getEntityFlaggings($flag, $entity, $account, $session_id);
  return !empty($flaggings) ? reset($flaggings) : NULL;
}