public function UserData::get in Zircon Profile 8
Same name and namespace in other branches
- 8.0 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.
integer $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 37 - Contains \Drupal\user\UserData.
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 was 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;
}
elseif (isset($uid)) {
$return = array();
foreach ($result as $record) {
$return[$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
elseif (isset($name)) {
$return = array();
foreach ($result as $record) {
$return[$record->uid] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
else {
$return = array();
foreach ($result as $record) {
$return[$record->uid][$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
}