hook_update_last_removed()
Return a number which is no longer available as hook_update_N(). If you remove some update functions from your mycustommodule.install file, you should notify Drupal of those missing functions. This way, Drupal can ensure that no update is accidentally skipped.
Parameters
none
Return value
An integer, corresponding to hook_update_N() which has been removed from mycustommodule.install.
Usage sample
function hook_update_last_removed() {
// We've removed the 6.x-1.x version of mycustommodule, including database updates.
// The next update function ismycustommodule_update_6200()
return 6105;
}
hook_enable()
This hook is optional. Perform necessary actions after module is enabled. The hook is called everytime module is enabled.
Parameters
none
Return value
none
Usage sample
function hook_enable() {
mycustommodule_cache_rebuild();
}
hook_disable()
This hook is optional. Perform necessary actions before module is disabled. The hook is called everytime module is disabled.
Parameters
none
Return value
none
Usage sample
function hook_disable() {
mycustommodule_cache_rebuild();
}
hook_schema()
We use hook_schema() to define the current version of the database schema.
During the module installation process in Drupal, you can create DB tables using .sql query or you can use hook_schema() to create tables and fields. A Drupal schema definition is an array structure representing one or more tables and their related keys and indexes. A schema is defined by hook_schema() which must be located in your module’s .install file.
By implementing hook_schema() and specifying the tables your module declares, you can create and drop DB tables on all supported database engines. You don’t have to deal with the different SQL dialects for table creation and alteration of the supported database engines.
Parameters
No parameters for this function
Return value
A schema definition array. For each element of the array, the key is a table name and the value is a table structure definition.
Usage sample:
function page_title_schema() {
$schema['page_title'] = array(
'fields' => array(
'type' => array('type' => 'varchar', 'length' => 15, 'not null' => TRUE, 'default' => 'node'),
'id' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'page_title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '')
),
'primary key' => array('type', 'id'),
);
return $schema;
}
If you have multiple table, add another $schema array. Below is an example of multiple tables installed during your installation process.
function module_schema(){
$schema= array();
$schema["table1"] = array(...); // table1 schema
$schema["table2"] = array(...);// table 2 schem
etc.
return $schema;
}
to install both tables, you just use hook_install to install schema
function module_install(){
drupal_install_schema('module');
// where module is name of your module
}
hook_access($op, $node, $account)
Define access restrictions. This hook allows node modules to limit access to the node types they define.
Parameters
$op The operation to be performed. Possible values:
- “create”
- “delete”
- “update”
- “view”
$node The node on which the operation is to be performed, or, if it does not yet exist, the type of node to be created.
$account A user object representing the user for whom the operation is to be performed.
Return value
TRUE if the operation is to be allowed; FALSE if the operation is to be denied; NULL to not override the settings in the node_access table, or access control modules.
Codes:
function hook_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create my sales', $account);
}if ($op == 'update' || $op == 'delete') {
if (user_access('edit her sales', $account) && ($account->uid == $node->uid)) {
return TRUE;
}
}
}
Usage sample:
<?php
CREATE TABLE node_sample (
vid int(10) unsigned NOT NULL default '0',
nid int(10) unsigned NOT NULL default '0',
color varchar(255) NOT NULL default '',
quantity int(10) unsigned NOT NULL default '0',
PRIMARY KEY (vid, nid),
KEY `node_sample_nid` (nid))
?>
function node_sample_access($op, $node, $account) {
if ($op == 'create') {
return user_access('create sample content', $account);
}
if ($op == 'update') {
if (user_access('edit any sample content', $account) || (user_access('edit own sample content', $account) && ($account->uid == $node->uid))) {
return TRUE;
}
}
if ($op == 'delete') {
if (user_access('delete any sample content', $account) || (user_access('delete own sample content', $account) && ($account->uid == $node->uid))) {
return TRUE;
}
}
}