public function FlagService::populateFlaggerDefaults in Flag 8.4
Set up values for the flagger user account and session.
This is a helper method for functions that allow the flagger account to be omitted to mean the current user.
If you always want the current user, you can use this as follows:
$account = $session_id = NULL;
\Drupal::service('flag')
->populateFlaggerDefaults($account, $session_id);
Parameters
\Drupal\Core\Session\AccountInterface $account: A user account, or a variable set to NULL (rather than the constant NULL) to get the current user assigned to it.
string $session_id: A session ID, or a variable set to NULL to get the session ID assigned to it in the case that the user also unspecified and anonymous. If $account is not NULL and is the anonymous user, then this must be specified, and in this case, it is the caller's responsibility to ensure that the session is properly started.
Throws
\LogicException Throws an exception is $account is specified and is the anonymous user, but $session_id is NULL.
Overrides FlagServiceInterface::populateFlaggerDefaults
3 calls to FlagService::populateFlaggerDefaults()
- FlagService::flag in src/
FlagService.php - Flags the given entity given the flag and entity objects.
- FlagService::getFlagging in src/
FlagService.php - Get a single flagging for given a flag and entity.
- FlagService::unflag in src/
FlagService.php - Unflags the given entity for the given flag.
File
- src/
FlagService.php, line 108
Class
- FlagService
- Flag service.
Namespace
Drupal\flagCode
public function populateFlaggerDefaults(AccountInterface &$account = NULL, &$session_id = NULL) {
// Note that the $account parameter must be explicitly set to be passed by
// reference for the case when the variable is NULL rather than an object;
// also, it must be optional to allow a variable that is NULL to pass the
// type-hint check.
if (!isset($account)) {
// If there isn't an account, set it to the current user.
$account = $this->currentUser;
// If the user is anonymous, get the session ID. Note that this does not
// always mean that the session is started. Session is started explicitly
// from FlagService->ensureSession() method.
if (!isset($session_id) && $account
->isAnonymous()) {
$session_id = $this->sessionManager
->getId();
}
}
elseif ($account
->isAnonymous() && $session_id === NULL) {
throw new \LogicException('Anonymous users must be identified by session_id');
}
}