class UserData

Same name in this branch
  1. 9 core/modules/user/src/Plugin/views/field/UserData.php \Drupal\user\Plugin\views\field\UserData
  2. 9 core/modules/user/src/Plugin/migrate/destination/UserData.php \Drupal\user\Plugin\migrate\destination\UserData
Same name in other branches
  1. 8.9.x core/modules/user/src/Plugin/views/field/UserData.php \Drupal\user\Plugin\views\field\UserData
  2. 8.9.x core/modules/user/src/Plugin/migrate/destination/UserData.php \Drupal\user\Plugin\migrate\destination\UserData
  3. 8.9.x core/modules/user/src/UserData.php \Drupal\user\UserData
  4. 10 core/modules/user/src/Plugin/views/field/UserData.php \Drupal\user\Plugin\views\field\UserData
  5. 10 core/modules/user/src/Plugin/migrate/destination/UserData.php \Drupal\user\Plugin\migrate\destination\UserData
  6. 10 core/modules/user/src/UserData.php \Drupal\user\UserData
  7. 11.x core/modules/user/src/Plugin/views/field/UserData.php \Drupal\user\Plugin\views\field\UserData
  8. 11.x core/modules/user/src/Plugin/migrate/destination/UserData.php \Drupal\user\Plugin\migrate\destination\UserData
  9. 11.x core/modules/user/src/UserData.php \Drupal\user\UserData

Defines the user data service.

Hierarchy

Expanded class hierarchy of UserData

1 file declares its use of UserData
UserData.php in core/modules/user/src/Plugin/migrate/destination/UserData.php
1 string reference to 'UserData'
user.services.yml in core/modules/user/user.services.yml
core/modules/user/user.services.yml
1 service uses UserData
user.data in core/modules/user/user.services.yml
Drupal\user\UserData

File

core/modules/user/src/UserData.php, line 10

Namespace

Drupal\user
View source
class UserData implements UserDataInterface {
    
    /**
     * The database connection to use.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $connection;
    
    /**
     * Constructs a new user data service.
     *
     * @param \Drupal\Core\Database\Connection $connection
     *   The database connection to use.
     */
    public function __construct(Connection $connection) {
        $this->connection = $connection;
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }
    
    /**
     * {@inheritdoc}
     */
    public function set($module, $uid, $name, $value) {
        $serialized = (int) (!is_scalar($value));
        if ($serialized) {
            $value = serialize($value);
        }
        $this->connection
            ->merge('users_data')
            ->keys([
            'uid' => $uid,
            'module' => $module,
            'name' => $name,
        ])
            ->fields([
            'value' => $value,
            'serialized' => $serialized,
        ])
            ->execute();
    }
    
    /**
     * {@inheritdoc}
     */
    public function delete($module = NULL, $uid = NULL, $name = NULL) {
        $query = $this->connection
            ->delete('users_data');
        // Cast scalars to array so we can consistently use an IN condition.
        if (isset($module)) {
            $query->condition('module', (array) $module, 'IN');
        }
        if (isset($uid)) {
            $query->condition('uid', (array) $uid, 'IN');
        }
        if (isset($name)) {
            $query->condition('name', (array) $name, 'IN');
        }
        $query->execute();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
UserData::$connection protected property The database connection to use.
UserData::delete public function Deletes data stored for a user account. Overrides UserDataInterface::delete
UserData::get public function Returns data stored for a user account. Overrides UserDataInterface::get
UserData::set public function Stores data for a user account. Overrides UserDataInterface::set
UserData::__construct public function Constructs a new user data service.

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