public function UserData::get in Drupal 8
Same name and namespace in other branches
- 9 core/modules/user/src/UserData.php \Drupal\user\UserData::get()
Returns data stored for a user account.
Parameters
string $module: The name of the module the data is associated with.
int $uid: (optional) The user account ID the data is associated with.
string $name: (optional) The name of the data key.
Return value
mixed|array The requested user account data, depending on the arguments passed:
- For $module, $name, and $uid, the stored value is returned, or NULL if no value was found.
- For $module and $uid, an associative array is returned that contains the stored data name/value pairs.
- For $module and $name, an associative array is returned whose keys are user IDs and whose values contain the stored values.
- For $module only, an associative array is returned that contains all existing data for $module in all user accounts, keyed first by user ID and $name second.
Overrides UserDataInterface::get
File
- core/
modules/ user/ src/ UserData.php, line 32
Class
- UserData
- Defines the user data service.
Namespace
Drupal\userCode
public function get($module, $uid = NULL, $name = NULL) {
$query = $this->connection
->select('users_data', 'ud')
->fields('ud')
->condition('module', $module);
if (isset($uid)) {
$query
->condition('uid', $uid);
}
if (isset($name)) {
$query
->condition('name', $name);
}
$result = $query
->execute();
// If $module, $uid, and $name were passed, return the value.
if (isset($name) && isset($uid)) {
$result = $result
->fetchAllAssoc('uid');
if (isset($result[$uid])) {
return $result[$uid]->serialized ? unserialize($result[$uid]->value) : $result[$uid]->value;
}
return NULL;
}
$return = [];
// If $module and $uid were passed, return data keyed by name.
if (isset($uid)) {
foreach ($result as $record) {
$return[$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
// If $module and $name were passed, return data keyed by uid.
if (isset($name)) {
foreach ($result as $record) {
$return[$record->uid] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
// If only $module was passed, return data keyed by uid and name.
foreach ($result as $record) {
$return[$record->uid][$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}