UserData::get

public UserData::get($module, $uid = NULL, $name = NULL)

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\user

Code

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;
  }
  // If $module and $uid was passed, return the name/value pairs.
  elseif (isset($uid)) {
    $return = array();
    foreach ($result as $record) {
      $return[$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
    }
    return $return;
  }
  // If $module and $name was passed, return the uid/value pairs.
  elseif (isset($name)) {
    $return = array();
    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.
  else {
    $return = array();
    foreach ($result as $record) {
      $return[$record->uid][$record->name] = ($record->serialized ? unserialize($record->value) : $record->value);
    }
    return $return;
  }
}
doc_Drupal
2016-10-29 09:52:29
Comments
Leave a Comment

Please login to continue.