You are here

public function MongodbUserData::get in MongoDB 8

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

mongodb_user/src/MongodbUserData.php, line 46
Contains \Drupal\mongodb_user\MongodbUserData.

Class

MongodbUserData

Namespace

Drupal\mongodb_user

Code

public function get($module, $uid = NULL, $name = NULL) {
  $args = func_get_args();
  $result = $this->mongo
    ->get('user_data')
    ->find($this
    ->getQuery($args));
  switch (isset($uid) + isset($name)) {
    case 2:
      foreach ($result as $row) {
        return $row['value'];
      }
      return NULL;
    case 1:
      $return = [];
      $key_field = isset($uid) ? 'name' : 'uid';
      foreach ($result as $row) {
        $return[$row[$key_field]] = $row['value'];
      }
      return $return;
    case 0:
      $return = [];
      foreach ($result as $row) {
        $return[$row['uid']][$row['name']] = $row['value'];
      }
      return $return;
  }
}