user.install
Same filename in other branches
Install, update and uninstall functions for the user module.
File
-
core/
modules/ user/ user.install
View source
<?php
/**
* @file
* Install, update and uninstall functions for the user module.
*/
/**
* Implements hook_schema().
*/
function user_schema() : array {
$schema['users_data'] = [
'description' => 'Stores module data as key/value pairs per user.',
'fields' => [
'uid' => [
'description' => 'The {users}.uid this record affects.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'module' => [
'description' => 'The name of the module declaring the variable.',
'type' => 'varchar_ascii',
'length' => DRUPAL_EXTENSION_NAME_MAX_LENGTH,
'not null' => TRUE,
'default' => '',
],
'name' => [
'description' => 'The identifier of the data.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'value' => [
'description' => 'The value.',
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
],
'serialized' => [
'description' => 'Whether value is serialized.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'default' => 0,
],
],
'primary key' => [
'uid',
'module',
'name',
],
'indexes' => [
'module' => [
'module',
],
'name' => [
'name',
],
],
'foreign keys' => [
'data_user' => [
'table' => 'users',
'columns' => [
'uid' => 'uid',
],
],
],
];
return $schema;
}
/**
* Implements hook_install().
*/
function user_install() : void {
$storage = \Drupal::entityTypeManager()->getStorage('user');
// Insert a row for the anonymous user.
$storage->create([
'uid' => 0,
'status' => 0,
'name' => '',
])
->save();
// We need some placeholders here as name and mail are unique.
// This will be changed by the settings form in the installer.
$storage->create([
'uid' => 1,
'name' => 'placeholder-for-uid-1',
'mail' => 'placeholder-for-uid-1',
'status' => TRUE,
])
->save();
}
/**
* Implements hook_requirements().
*/
function user_requirements($phase) : array {
if ($phase !== 'runtime') {
return [];
}
$return = [];
$result = (bool) \Drupal::entityQuery('user')->accessCheck(FALSE)
->condition('uid', 0)
->range(0, 1)
->execute();
if ($result === FALSE) {
$return['anonymous user'] = [
'title' => t('Anonymous user'),
'description' => t('The anonymous user does not exist. See the <a href=":url">restore the anonymous (user ID 0) user record</a> for more information', [
':url' => 'https://www.drupal.org/node/1029506',
]),
'severity' => REQUIREMENT_WARNING,
];
}
$query = \Drupal::database()->select('users_field_data');
$query->addExpression('LOWER(mail)', 'lower_mail');
$query->isNotNull('mail');
$query->groupBy('lower_mail');
$query->having('COUNT(uid) > :matches', [
':matches' => 1,
]);
$conflicts = $query->countQuery()
->execute()
->fetchField();
if ($conflicts > 0) {
$return['conflicting emails'] = [
'title' => t('Conflicting user emails'),
'description' => t('Some user accounts have email addresses that differ only by case. For example, one account might have alice@example.com and another might have Alice@Example.com. See <a href=":url">Conflicting User Emails</a> for more information.', [
':url' => 'https://www.drupal.org/node/3486109',
]),
'severity' => REQUIREMENT_WARNING,
];
}
return $return;
}
/**
* Implements hook_update_last_removed().
*/
function user_update_last_removed() : int {
return 10000;
}
Functions
Title | Deprecated | Summary |
---|---|---|
user_install | Implements hook_install(). | |
user_requirements | Implements hook_requirements(). | |
user_schema | Implements hook_schema(). | |
user_update_last_removed | Implements hook_update_last_removed(). |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.