function UserData::get

Same name in other branches
  1. 9 core/modules/user/src/UserData.php \Drupal\user\UserData::get()
  2. 8.9.x core/modules/user/src/UserData.php \Drupal\user\UserData::get()
  3. 11.x core/modules/user/src/UserData.php \Drupal\user\UserData::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 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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.