Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Following a simple example taken from Pro Drupal Development Second Edition:
<?php
/**
* Implementation of hook_install().
*/
function markednode_install() {
$field = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'initial' => 0, // Sets initial value for preexisting nodes.
'description' => t('Whether the node has been marked by the
markednode module.'),
);
// Create a regular index called 'marked' on the field named 'marked'.
$keys['indexes'] = array(
'marked' => array('marked')
);
$ret = array(); // Results of the SQL calls will be stored here.
db_add_field($ret, 'node', 'marked', $field, $keys);
}
/**
* Implementation of hook_schema_alter(). We alter $schema by reference.
*
* @param $schema
* The system-wide schema collected by drupal_get_schema().
*/
function markednode_schema_alter(&$schema) {
// Add field to existing schema.
$schema['node']['fields']['marked'] = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => t('Whether the node has been marked by the
markednode module.'),
);
}
?>
The former is accomplished with hook_install, which install first the extended table.
The latter with hook_schema_alter, which cast an exception since the field already exists.
I would appreciate to know if I am doing something wrong, but I'm not sure about this drupal inconsistency, on an install, a module should invoke all the _schema_alter hooks.. EXCEPTING own hook.
Does it makes sense ?
Comments