Heya everyone
In my use case, I retrieve user IDs through a view, save the UIDs in a list, and iterate over them. At a later stage, I need to check if a user has a specific role. However, upon reviewing the code behind the "Role of user" condition, I noticed that checking for a role is only possible when an AccountInterface object, a UserInterface object, or a numeric value (presumably the UID) is available for loading the user account. Here’s the relevant code:
public function evaluate(): bool {
if ($account = $this->loadUserAccount()) {
$userRoles = $account->getRoles();
$result = in_array($this->configuration['role'], $userRoles, TRUE);
$debug = 1;
return $this->negationCheck($result);
}
return FALSE;
}
protected function loadUserAccount(): ?UserInterface {
$account = $this->tokenService->getOrReplace($this->configuration['account']);
if ($account instanceof AccountInterface) {
if (!($account instanceof UserInterface)) {
$account = $account->id();
}
}
elseif (!is_numeric($account)) {
$account = $this->tokenService->replaceClear($this->configuration['account']);
// @see user_tokens().
if ((string) $account === 'not yet assigned') {
$account = 0;
}
}
if (is_numeric($account)) {
/**
* @var \Drupal\user\UserInterface $account
*/
try {
$account = $this->entityTypeManager->getStorage('user')->load($account);
}
catch (InvalidPluginDefinitionException | PluginNotFoundException $e) {
$account = NULL;
}
}
return ($account instanceof UserInterface) ? $account : NULL;
}
The issue I’m facing is that the UID returned from the view is a string. While I could create a separate action to load the user entity based on the view results, I would prefer to avoid adding another step in workflows to keep them simpler and less complex.
My question is: why isn’t the "Role of user" condition designed to support loading a user account directly from a string value? Alternatively, what is the reasoning behind restricting it to AccountInterface, UserInterface, or numeric values for loading the account?
Any insights would be greatly appreciated!