public function FlagServiceInterface::flag in Flag 8.4
Flags the given entity given the flag and entity objects.
To programatically create a flagging between a flag and an article:
$flag_service = \Drupal::service('flag');
$flag = $flag_service
->getFlagById('bookmark');
$node = Node::load($node_id);
$flag_service
->flag($flag, $node);
Parameters
\Drupal\flag\FlagInterface $flag: The flag entity.
\Drupal\Core\Entity\EntityInterface $entity: The entity to flag.
\Drupal\Core\Session\AccountInterface $account: (optional) The account of the user flagging the entity. If not given, the current user is used.
string $session_id: (optional) The session ID. If $account is NULL and the current user is anonymous, then this can also be omitted to use the current session. to identify an anonymous user.
Return value
\Drupal\flag\FlagInterface|null The flagging.
Throws
\LogicException An exception is thrown if the given flag, entity, and account are not compatible in some way:
- The flag applies to a different entity type from the given entity.
- The flag does not apply to the entity's bundle.
- The entity is already flagged with this flag by the user.
- The user is anonymous but not uniquely identified by session_id.
1 method overrides FlagServiceInterface::flag()
- FlagService::flag in src/
FlagService.php - Flags the given entity given the flag and entity objects.
File
- src/
FlagServiceInterface.php, line 221
Class
- FlagServiceInterface
- Flag service interface.
Namespace
Drupal\flagCode
public function flag(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL, $session_id = NULL);