user.install

Same filename in other branches
  1. 7.x modules/user/user.install
  2. 8.9.x core/modules/user/user.install
  3. 10 core/modules/user/user.install
  4. 11.x core/modules/user/user.install

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.
 */
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;

/**
 * Implements hook_schema().
 */
function user_schema() {
    $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() {
    $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_update_last_removed().
 */
function user_update_last_removed() {
    return 8100;
}

/**
 * Change the users table to use an serial uid field.
 */
function user_update_9301(&$sandbox) {
    if (!\Drupal::entityTypeManager()->getStorage('user') instanceof SqlContentEntityStorage) {
        return t('The user entity storage is not using an SQL storage, update skipped.');
    }
    $connection = \Drupal::database();
    if ($connection->databaseType() === 'sqlsrv') {
        return t('The Microsoft SQL Server does not support user_update_9301() because it causes data loss.');
    }
    if ($connection->databaseType() === 'mysql') {
        $sql_mode = $connection->query("SELECT @@sql_mode;")
            ->fetchField();
        $connection->query("SET sql_mode = '{$sql_mode},NO_AUTO_VALUE_ON_ZERO'");
        $new_keys = [];
    }
    else {
        $new_keys = [
            'primary key' => [
                'uid',
            ],
        ];
        $connection->schema()
            ->dropPrimaryKey('users');
    }
    $connection->schema()
        ->changeField('users', 'uid', 'uid', [
        'type' => 'serial',
        'not null' => TRUE,
    ], $new_keys);
    if (isset($sql_mode)) {
        $connection->query("SET sql_mode = '{$sql_mode}'");
    }
    // Update the last installed schema to reflect the change of field type.
    $installed_storage_schema = \Drupal::keyValue('entity.storage_schema.sql');
    $field_schema_data = $installed_storage_schema->get('user.field_schema_data.uid');
    $field_schema_data['users']['fields']['uid']['type'] = 'serial';
    $installed_storage_schema->set('user.field_schema_data.uid', $field_schema_data);
    // The new PostgreSQL sequence for the uid field needs to start with the last
    // used user ID + 1 and the sequence must be owned by uid field.
    // @todo https://drupal.org/i/3028706 implement a generic fix.
    if ($connection->driver() == 'pgsql') {
        $maximum_uid = $connection->query('SELECT MAX([uid]) FROM {users}')
            ->fetchField();
        $seq = $connection->makeSequenceName('users', 'uid');
        $connection->query("ALTER SEQUENCE " . $seq . " RESTART WITH " . ($maximum_uid + 1) . " OWNED BY {users}.uid");
    }
}
// Do not add further hook_update_N() implementations to this file.
// user_update_10000() has been added to 10.0.x which makes adding new updates
// extremely complex.

Functions

Title Deprecated Summary
user_install Implements hook_install().
user_schema Implements hook_schema().
user_update_9301 Change the users table to use an serial uid field.
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.