quotes.install in Quotes 7
Same filename and directory in other branches
Handles installation and updates for the quotes module.
@copyright Copyright (c) 2003-2007 Jim Riggs. All rights reserved. @author Jim Riggs <drupal at jim and lissa dot com>
File
quotes.installView source
<?php
/**
* @file
* Handles installation and updates for the quotes module.
*
* @copyright Copyright (c) 2003-2007 Jim Riggs. All rights reserved.
* @author Jim Riggs <drupal at jim and lissa dot com>
*/
/* ********************************************************************
* Drupal Hooks
* ********************************************************************
*/
/**
* Implements hook_schema().
*/
function quotes_schema() {
$schema = array();
$schema['quotes'] = array(
'module' => 'Quotes',
'description' => 'Extra node data.',
'fields' => array(
'nid' => array(
'description' => 'Node identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'disp-width' => '10',
),
'vid' => array(
'description' => 'Version identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'disp-width' => '10',
),
'aid' => array(
'description' => 'Author identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'disp-width' => '10',
),
'citation' => array(
'description' => 'Source of the quote.',
'type' => 'text',
'not null' => FALSE,
),
'promote' => array(
'description' => 'Status.',
'type' => 'int',
'size' => 'tiny',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'vid',
),
'indexes' => array(
'quotes_nid' => array(
'nid',
),
'quotes_promote' => array(
'promote',
),
'quotes_aid' => array(
'aid',
),
),
);
$schema['quotes_blocks'] = array(
'module' => 'Quotes',
'description' => 'Quotes blocks data.',
'fields' => array(
'bid' => array(
'description' => 'Block number',
'type' => 'serial',
'not null' => TRUE,
),
'block_type' => array(
'description' => 'Type of block',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
),
'vid' => array(
'description' => 'Version id',
'type' => 'int',
'not null' => TRUE,
),
'count' => array(
'description' => 'Number of quotes in the block.',
'type' => 'int',
'not null' => FALSE,
'default' => 1,
),
'cron_interval' => array(
'description' => 'Cron frequency',
'type' => 'int',
'not null' => TRUE,
),
'cron_step' => array(
'description' => 'Cron step',
'type' => 'int',
'not null' => TRUE,
),
'cron_last' => array(
'description' => 'Last Cron run time',
'type' => 'int',
'not null' => TRUE,
),
'show_titles' => array(
'description' => 'Show titles.',
'type' => 'int',
'size' => 'small',
'not null' => FALSE,
'default' => 0,
'disp-width' => '6',
),
'show_citation' => array(
'description' => 'Show citation.',
'type' => 'int',
'size' => 'small',
'not null' => FALSE,
'default' => 0,
),
'max_length' => array(
'description' => 'Maximum length of quote in block.',
'type' => 'int',
'not null' => FALSE,
'default' => 0,
'disp-width' => '11',
),
'rand_freq' => array(
'description' => 'Display frequency for random blocks.',
'type' => 'int',
'not null' => TRUE,
'default' => 100,
),
'name' => array(
'description' => 'Name of this block',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'nid_filter' => array(
'description' => 'Node filter',
'type' => 'text',
'not null' => TRUE,
),
'aid_filter' => array(
'description' => 'Author filter',
'type' => 'text',
'not null' => TRUE,
),
'rid_filter' => array(
'description' => 'Role filter',
'type' => 'text',
'not null' => TRUE,
),
'uid_filter' => array(
'description' => 'User filter',
'type' => 'text',
'not null' => TRUE,
),
'tid_filter' => array(
'description' => 'Term filter',
'type' => 'text',
'not null' => TRUE,
),
'view_text' => array(
'description' => 'Text for the "view" link.',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
'more_text' => array(
'description' => 'Text for the "more" link.',
'type' => 'varchar',
'length' => 64,
'not null' => FALSE,
),
),
'primary key' => array(
'bid',
),
'unique keys' => array(
'name' => array(
'name',
),
),
);
$schema['quotes_authors'] = array(
'module' => 'Quotes',
'description' => 'Quotes authors data.',
'fields' => array(
'aid' => array(
'description' => 'Author identifier.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'disp-width' => '10',
),
'name' => array(
'description' => 'Author of the quote.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'bio' => array(
'description' => "Author's biography.",
'type' => 'text',
'not null' => FALSE,
),
),
'primary key' => array(
'aid',
),
'unique keys' => array(
'name' => array(
'name',
),
),
);
return $schema;
}
/**
* Implements hook_update_last_removed().
*/
function quotes_update_last_removed() {
// Removed the 5.x and 6.x version of quotes, including database updates.
// The next update function is quotes_update_7001().
return 6107;
}
/**
* Implements hook_install().
*/
function quotes_install() {
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['quotes']);
// Create a default vocabulary named "Quotes" for our content.
_quotes_taxonomy_fields();
// Set default variables.
variable_set('quotes_author_bio', FALSE);
variable_set('quotes_author_link', FALSE);
variable_set('quotes_block_citation', FALSE);
variable_set('quotes_leader', '—');
variable_set('quotes_quick_nav', TRUE);
variable_set('quotes_per_page', 10);
variable_set('quotes_show_myquotes', TRUE);
variable_set('quotes_showlink', TRUE);
variable_set('quotes_user_recent', FALSE);
variable_set('quotes_edit_link', TRUE);
return t('The default vocabulary "Quotes" has been added. You may now setup category terms to categorize quotes');
}
/**
* Change table quotes_author column name from text to varchar 255.
*
* Change unique keys index to match name column.
*/
function quotes_update_7000() {
$sql = db_select('quotes_authors', 'qa');
$sql
->fields('qa', array(
'aid',
'name',
));
$q_alias = $sql
->leftjoin('quotes', 'q', "q.aid = qa.aid");
$count_alias = $sql
->addExpression('COUNT(q.nid)', 'count');
$sql
->groupBy('name')
->orderBy('name', 'DESC');
$sql = db_select('quotes_authors', 'qa');
$sql
->fields('qa', array(
'aid',
'name',
));
$q_alias = $sql
->leftjoin('quotes', 'q', "q.aid = qa.aid");
$count_alias = $sql
->addExpression('COUNT(q.nid)', 'count');
$sql
->groupBy('name')
->orderBy('name', 'DESC');
$result = $sql
->execute()
->fetchAll();
foreach ($result as $key => $value) {
$record = $value;
if (drupal_strlen($record->name) > 255) {
$toolong[] = $record->name;
}
}
if (isset($toolong)) {
if (count($toolong) < 2) {
$prefix = 'There is 1 author';
}
else {
$prefix = "There are " . count($toolong) . " authors";
}
throw new DrupalUpdateException($prefix . ' over 255 characters in lenght. After correcting this issue return to the update processor and run again.');
}
db_drop_index('quotes_authors', 'name');
db_change_field('quotes_authors', 'name', 'name', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
));
db_add_unique_key('quotes_authors', 'name', array(
'name',
));
}
/**
* Update taxonomy or add a default vocabulary for quotes.
*/
function quotes_update_7001() {
// Get only the quotes module vocabulary.
$vid = db_query('SELECT * FROM {taxonomy_vocabulary} WHERE name = :name', array(
':name' => 'quotes',
))
->fetchObject();
if ($vid) {
// Change our vocabulary machine name.
db_update('taxonomy_vocabulary')
->fields(array(
'machine_name' => 'quotes',
))
->condition('vid', $vid->vid)
->execute();
// Get our vocabulary by our new machine_name.
$vocabulary = taxonomy_vocabulary_machine_name_load('quotes');
// Install our taxonomy fields under new vocabulary machine_name
_quotes_taxonomy_fields();
// Now run the remaining taxonomy updates.
// First copy our old machine_name field data to the new machine_name.
$instance = "taxonomy_{$vid->machine_name}";
_quotes_update_taxonomy($instance);
// Delete the field
field_delete_field($instance);
// Now take care of taxonomyextra.
_quotes_update_taxonomy('taxonomyextra');
return t('Quotes taxonomy has been updated.');
}
else {
// If there isn't a vocabulary set up the default.
_quotes_taxonomy_fields();
return t('The default vocabulary "Quotes" has been added. You may now setup category terms to categorize quotes');
}
}
/**
* Implements hook_uninstall().
*/
function quotes_uninstall() {
// Gather all the content that might have been created while this
// module was enabled. Simple selects still use db_query().
// http://api.drupal.org/api/function/db_query/7
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(
':type' => 'quotes',
));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
// Delete all the nodes at once.
// http://api.drupal.org/api/function/node_delete_multiple/7
node_delete_multiple($nids);
// Loop over any remaining field instances attached to the node_example
// content type (such as the body field) and delete them individually.
// http://api.drupal.org/api/function/field_delete_field/7
$instances = field_info_instances('node', 'quotes');
foreach ($instances as $instance_name => $instance) {
field_delete_instance($instance);
}
// Delete our content type.
// http://api.drupal.org/api/function/node_type_delete/7
node_type_delete('quotes');
// Purge all field infromation.
// http://api.drupal.org/api/function/field_purge_batch/7
field_purge_batch(1000);
// Delete all our variable we set.
db_query("DELETE FROM {variable} WHERE name LIKE ('quotes_%')");
// Delete the extra_fields.
variable_del('field_bundle_settings_node__quotes');
// Delete all our blocks.
db_query("DELETE FROM {block} WHERE module='quotes'");
// Delete our vocabulary and all terms under it.
$names = db_query('SELECT vid FROM {taxonomy_vocabulary} WHERE name = :name', array(
':name' => 'quotes',
))
->fetchField();
taxonomy_vocabulary_delete($names);
// Include a uninstall message.
drupal_set_message(st('quotes module uninstalled.'));
}
/**
* Install a default vocabulary for quotes.
*/
function _quotes_taxonomy_fields() {
// Create a default vocabulary named "Quotes" for our content.
$description = st('A Collection of Quotes.');
$help = st('Enter a single or comma-separated list of words to categorize this quote.');
// Get only the quotes module vocabulary.
$vid = db_query('SELECT vid FROM {taxonomy_vocabulary} WHERE name = :name', array(
':name' => 'quotes',
))
->fetchField();
if ($vid) {
db_update('taxonomy_vocabulary')
->fields(array(
'name' => st('Quotes'),
'description' => $description,
'machine_name' => 'quotes',
))
->condition('vid', $vid)
->execute();
// Get our vocabulary by our new machine_name.
$vocabulary = taxonomy_vocabulary_machine_name_load('quotes');
}
else {
$vocabulary = (object) array(
'name' => st('Quotes'),
'description' => $description,
'machine_name' => 'quotes',
'help' => $help,
);
taxonomy_vocabulary_save($vocabulary);
}
$field = array(
'field_name' => 'taxonomy_' . $vocabulary->machine_name,
'type' => 'taxonomy_term_reference',
// Set cardinality to unlimited for tagging.
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $vocabulary->machine_name,
'parent' => 0,
),
),
),
);
field_create_field($field);
$instance = array(
'field_name' => 'taxonomy_' . $vocabulary->machine_name,
'entity_type' => 'node',
'label' => 'Category',
'bundle' => 'quotes',
'description' => $help,
'widget' => array(
'type' => 'taxonomy_autocomplete',
'weight' => -4,
),
'display' => array(
'default' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
),
'teaser' => array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
),
),
);
field_create_instance($instance);
}
/*
* Update the quotes taxonomy fields.
*/
function _quotes_update_taxonomy($instance) {
$revision_name = "field_revision_{$instance}";
if (!db_table_exists($revision_name)) {
return;
}
$query = db_select($revision_name, 'tr');
$query
->addField('tr', 'deleted');
$query
->addField('tr', 'entity_id', 'nid');
$query
->addField('tr', 'revision_id', 'vid');
$query
->addField('tr', 'language');
$query
->addField('tr', 'delta');
$query
->addField('tr', "{$instance}_tid", 'tid');
$fields = $query
->condition('bundle', 'quotes')
->execute();
if ($fields) {
foreach ($fields as $record) {
$field_name = 'taxonomy_quotes';
$table_name = "field_data_{$field_name}";
$revision_name = "field_revision_{$field_name}";
$value_column = $field_name . '_tid';
// Column names and values in field storage are the same for current and
// revision.
$columns = array(
'entity_type',
'entity_id',
'revision_id',
'bundle',
'deleted',
'language',
'delta',
$value_column,
);
$values = array(
'node',
$record->nid,
$record->vid,
'quotes',
$record->deleted,
LANGUAGE_NONE,
$record->delta,
$record->tid,
);
// Insert rows into the revision table.
db_insert($revision_name)
->fields($columns)
->values($values)
->execute();
// Insert rows into the table.
db_insert($table_name)
->fields($columns)
->values($values)
->execute();
}
_update_7000_field_delete_instance($instance, 'node', 'quotes');
}
}
Functions
Name | Description |
---|---|
quotes_install | Implements hook_install(). |
quotes_schema | Implements hook_schema(). |
quotes_uninstall | Implements hook_uninstall(). |
quotes_update_7000 | Change table quotes_author column name from text to varchar 255. |
quotes_update_7001 | Update taxonomy or add a default vocabulary for quotes. |
quotes_update_last_removed | Implements hook_update_last_removed(). |
_quotes_taxonomy_fields | Install a default vocabulary for quotes. |
_quotes_update_taxonomy |