user.install

Same filename and directory in other branches
  1. 11.x core/modules/user/user.install
  2. 10 core/modules/user/user.install
  3. 9 core/modules/user/user.install
  4. 8.9.x core/modules/user/user.install
  5. 7.x 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\Database\SchemaDefinition\Column;
use Drupal\Core\Database\SchemaDefinition\ColumnSize;
use Drupal\Core\Database\SchemaDefinition\ForeignKey;
use Drupal\Core\Database\SchemaDefinition\Index;
use Drupal\Core\Database\SchemaDefinition\IntValue;
use Drupal\Core\Database\SchemaDefinition\PrimaryKey;
use Drupal\Core\Database\SchemaDefinition\Schema;
use Drupal\Core\Database\SchemaDefinition\SchemaDefinitionType;
use Drupal\Core\Database\SchemaDefinition\StringValue;
use Drupal\Core\Database\SchemaDefinition\Table;

/**
 * Implements hook_schema().
 */
function user_schema() : Schema {
  $tables[] = new Table(name: 'users_data', description: 'Stores module data as key/value pairs per user.', columns: [
    Column::int(name: 'uid', description: 'The {users}.uid this record affects.', unsigned: TRUE, notNull: TRUE, default: new IntValue(0)),
    Column::varcharAscii(name: 'module', description: 'The name of the module declaring the variable.', length: DRUPAL_EXTENSION_NAME_MAX_LENGTH, notNull: TRUE, default: new StringValue('')),
    Column::varcharAscii(name: 'name', description: 'The identifier of the data.', length: 128, notNull: TRUE, default: new StringValue('')),
    Column::blob(name: 'value', description: 'The value.', size: ColumnSize::Big, notNull: FALSE),
    Column::int(name: 'serialized', description: 'Whether value is serialized.', size: ColumnSize::Tiny, unsigned: TRUE, default: new IntValue(0)),
  ], primaryKey: new PrimaryKey([
    'uid',
    'module',
    'name',
  ]), indexes: [
    new Index(name: 'module', columns: [
      'module',
    ]),
    new Index(name: 'name', columns: [
      'name',
    ]),
  ], foreignKeys: [
    new ForeignKey(name: 'data_user', foreignTable: 'users', columns: [
      'uid',
    ], foreignColumns: [
      'uid',
    ]),
  ]);
  return new Schema(type: SchemaDefinitionType::Module, name: 'user', tables: $tables);
}

/**
 * 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_update_last_removed().
 */
function user_update_last_removed() : int {
  return 10000;
}

Functions

Title Deprecated Summary
user_install Implements hook_install().
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.