You are here

public function FlagServiceInterface::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.

See also

\Drupal\flag\FlagServiceInterface::getFlaggings()

1 method overrides FlagServiceInterface::getFlagging()
FlagService::getFlagging in src/FlagService.php
Get a single flagging for given a flag and entity.

File

src/FlagServiceInterface.php, line 71

Class

FlagServiceInterface
Flag service interface.

Namespace

Drupal\flag

Code

public function getFlagging(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL);