profile2.install in Profile 2 7.2
Same filename and directory in other branches
Install, update and uninstall functions for the profile module.
File
profile2.installView source
<?php
/**
* @file
* Install, update and uninstall functions for the profile module.
*/
/**
* Implements hook_install().
*/
function profile2_install() {
// Add an initial profile type, but only if installed manually. In case the
// module is installed via an installation profile, skip that.
if (!drupal_installation_attempted()) {
$type = entity_create('profile2_type', array(
'type' => 'main',
'label' => t('Main profile'),
'weight' => 0,
'data' => array(
'registration' => TRUE,
'revisions' => 0,
),
));
$type
->save();
user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array(
'edit own main profile',
'view own main profile',
));
drupal_set_message(t('A main profile type has been created and assigned to all users. Go to the <a href="!url">Profile types</a> page to add some fields or to configure further profile types.', array(
'!url' => url('admin/structure/profiles'),
)));
}
// Set module weight
profile2_update_7106();
}
/**
* Implements hook_schema().
*/
function profile2_schema() {
$schema['profile'] = array(
'description' => 'Stores profile items.',
'fields' => array(
'pid' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique profile item ID.',
),
'vid' => _profile2_vid_field(),
'type' => array(
'description' => 'The {profile_type}.type of this profile.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => NULL,
'description' => "The {users}.uid of the associated user.",
),
'label' => array(
'description' => 'A human-readable label for this profile.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'created' => array(
'description' => 'The Unix timestamp when the profile was created.',
'type' => 'int',
'not null' => FALSE,
),
'changed' => array(
'description' => 'The Unix timestamp when the profile was most recently saved.',
'type' => 'int',
'not null' => FALSE,
),
),
'indexes' => array(
'uid' => array(
'uid',
),
),
'foreign keys' => array(
'uid' => array(
'table' => 'users',
'columns' => array(
'uid' => 'uid',
),
),
'type' => array(
'table' => 'profile_type',
'columns' => array(
'type' => 'type',
),
),
),
'unique keys' => array(
'user_profile_type' => array(
'type',
'uid',
),
),
'primary key' => array(
'pid',
),
'unique keys' => array(
'vid' => array(
'vid',
),
),
);
$schema['profile_revision'] = _profile2_revision_table();
$schema['profile_type'] = array(
'description' => 'Stores information about all defined profile types.',
'fields' => array(
'id' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique profile type ID.',
),
'type' => array(
'description' => 'The machine-readable name of this profile type.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'label' => array(
'description' => 'The human-readable name of this profile type.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'weight' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'The weight of this profile type in relation to others.',
),
'data' => array(
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
'description' => 'A serialized array of additional data related to this profile type.',
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
// Set the default to ENTITY_CUSTOM without using the constant as it is
// not safe to use it at this point.
'default' => 0x1,
'size' => 'tiny',
'description' => 'The exportable status of the entity.',
),
'module' => array(
'description' => 'The name of the providing module if the entity has been defined in code.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
),
),
'primary key' => array(
'id',
),
'unique keys' => array(
'type' => array(
'type',
),
),
);
return $schema;
}
/**
* Implements hook_uninstall()
*/
function profile2_uninstall() {
// Select all available profile2 bundles from the database directly, instead
// of entity_load() call. See https://drupal.org/node/1330598 for details.
$types = db_select('profile_type', 'p')
->fields('p')
->execute()
->fetchAllAssoc('type');
foreach ($types as $name => $type) {
field_attach_delete_bundle('profile2', $name);
}
}
/**
* Define the vid (version ID) field for the {profile} table.
*
* Put it in a separate function in order to guarantee that we use the same
* definition in hook_schema() and hook_update_N().
*/
function _profile2_vid_field() {
$vid_field = array(
'description' => 'The current {profile_revision}.vid version identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => NULL,
);
return $vid_field;
}
/**
* Define the {profile_revision} table.
*
* Put it in a separate function in order to guarantee that we use the same
* definition in hook_schema() and hook_update_N().
*/
function _profile2_revision_table() {
$profile_revision = array(
'description' => 'Stores information about each saved version of a {profile}.',
'fields' => array(
'pid' => array(
'description' => 'The {profile} this version belongs to.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'vid' => array(
'description' => 'Primary Key: Unique identifier of this version.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'authorid' => array(
'description' => 'The {users}.uid that created this version.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'label' => array(
'description' => 'A human-readable label for this profile.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'log' => array(
'description' => 'The log entry explaining the changes in this version.',
'type' => 'text',
'not null' => TRUE,
'size' => 'big',
),
'timestamp' => array(
'description' => 'A Unix timestamp indicating when this version was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'pid' => array(
'pid',
),
'authorid' => array(
'authorid',
),
),
'primary key' => array(
'vid',
),
'foreign keys' => array(
'versioned_profile' => array(
'table' => 'profile',
'columns' => array(
'pid' => 'pid',
),
),
'version_uid' => array(
'table' => 'users',
'columns' => array(
'authorid' => 'uid',
),
),
),
);
return $profile_revision;
}
/**
* Add in the exportable entity db columns as required by the entity API.
*/
function profile2_update_7100() {
db_add_field('profile_type', 'status', array(
'type' => 'int',
'not null' => TRUE,
'default' => ENTITY_CUSTOM,
'size' => 'tiny',
'description' => 'The exportable status of the entity.',
));
db_add_field('profile_type', 'module', array(
'description' => 'The name of the providing module if the entity has been defined in code.',
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
));
}
/**
* Add a label column to profiles.
*/
function profile2_update_7101() {
db_add_field('profile', 'label', array(
'description' => 'A human-readable label for this profile.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
));
$types = db_select('profile_type', 'p')
->fields('p')
->execute()
->fetchAllAssoc('type');
// Initialize with the type label.
foreach ($types as $type_name => $type) {
db_update('profile')
->fields(array(
'label' => $type->label,
))
->condition('type', $type_name)
->execute();
}
}
/**
* Add a created and a changed column to profiles.
*/
function profile2_update_7102() {
db_add_field('profile', 'created', array(
'description' => 'The Unix timestamp when the profile was created.',
'type' => 'int',
'not null' => FALSE,
));
db_add_field('profile', 'changed', array(
'description' => 'The Unix timestamp when the profile was most recently saved.',
'type' => 'int',
'not null' => FALSE,
));
}
/**
* No changes for this schema update.
*/
function profile2_update_7103() {
}
/**
* Remove duplicate profile records in batches of 50, and add unique key on type + uid.
*/
function profile2_update_7104(&$sandbox) {
// Query to get duplicate profiles.
$query = db_select('profile', 'p1');
$query
->distinct();
$query
->fields('p1', array(
'pid',
));
$query
->join('profile', 'p2', 'p1.type = p2.type AND p1.uid = p2.uid AND p1.pid < p2.pid');
// Setup initial batch variables.
if (!isset($sandbox['progress'])) {
// The number of duplicate profiles deleted so far.
$sandbox['progress'] = 0;
// Total number of duplicate profiles that will be deleted.
$sandbox['total'] = $query
->execute()
->rowCount();
}
if ($sandbox['total']) {
// Query the next 50 profiles to be deleted.
$query
->range(0, 50);
$result = $query
->execute();
// Update progress of removing duplicate profiles.
$sandbox['progress'] = $sandbox['progress'] + $result
->rowCount();
// Delete duplicate profiles.
profile2_delete_multiple($result
->fetchCol());
// Update batch status.
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['total'];
}
else {
$sandbox['#finished'] = 1;
}
if ($sandbox['#finished'] >= 1) {
// Add a unique key on type + uid.
db_add_unique_key('profile', 'user_profile_type', array(
'type',
'uid',
));
return t('@total duplicate profiles were removed from the system.', array(
'@total' => $sandbox['progress'],
));
}
}
/**
* Add revisioning support.
*/
function profile2_update_7105() {
db_add_field('profile', 'vid', _profile2_vid_field());
$profile_revision = _profile2_revision_table();
db_create_table('profile_revision', $profile_revision);
// Add version id data to the profile.
db_query('UPDATE {profile} SET vid = pid');
// Add current revision data to the revision table.
db_query("INSERT INTO {profile_revision} (pid, vid, authorid, label, log, timestamp)\n SELECT pid, vid, uid, label, '', UNIX_TIMESTAMP()\n FROM {profile} WHERE uid IS NOT NULL");
db_add_unique_key('profile', 'vid', array(
'vid',
));
// Set the revisions flag for existing profiles.
foreach (profile2_get_types() as $profile_type) {
$profile_type->data['revisions'] = 0;
$profile_type
->save();
}
}
/**
* Increase the module weight.
*/
function profile2_update_7106() {
// Increase the weight of module in the system table,
// this is to make it higher than user_diff (if installed).
db_update('system')
->fields(array(
'weight' => 1,
))
->condition('name', 'profile2', '=')
->execute();
}
Functions
Name![]() |
Description |
---|---|
profile2_install | Implements hook_install(). |
profile2_schema | Implements hook_schema(). |
profile2_uninstall | Implements hook_uninstall() |
profile2_update_7100 | Add in the exportable entity db columns as required by the entity API. |
profile2_update_7101 | Add a label column to profiles. |
profile2_update_7102 | Add a created and a changed column to profiles. |
profile2_update_7103 | No changes for this schema update. |
profile2_update_7104 | Remove duplicate profile records in batches of 50, and add unique key on type + uid. |
profile2_update_7105 | Add revisioning support. |
profile2_update_7106 | Increase the module weight. |
_profile2_revision_table | Define the {profile_revision} table. |
_profile2_vid_field | Define the vid (version ID) field for the {profile} table. |