synonyms.install in Synonyms 7
Same filename and directory in other branches
Install, update, and uninstall functions for the Synonyms module.
File
synonyms.installView source
<?php
/**
* @file
* Install, update, and uninstall functions for the Synonyms module.
*/
/**
* Implements hook_schema().
*/
function synonyms_schema() {
$schema = array();
$schema['synonyms_settings'] = array(
'description' => 'Stores synonyms settings for all the entities and providers. Only enabled synonyms behavior implementations are included in this table.',
'fields' => array(
'entity_type' => array(
'description' => 'Entity type whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'bundle' => array(
'description' => 'Bundle name whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'provider' => array(
'description' => 'Provider name whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'behavior' => array(
'description' => 'Name of the synonyms behavior (ctools plugin), whose settings are stored in this row.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'settings_serialized' => array(
'description' => 'Settings of the specified synonyms behavior for the specified field instance.',
'type' => 'text',
'serialize' => TRUE,
'not null' => TRUE,
),
),
'unique keys' => array(
// We build 2 different indexes on the same column set because there are
// 2 different functions that may query this table and the columns they
// filter on may vary.
'behavior_implementation' => array(
'behavior',
'entity_type',
'bundle',
'provider',
),
'all_enabled' => array(
'entity_type',
'bundle',
'provider',
'behavior',
),
),
);
return $schema;
}
/**
* Implements hook_uninstall().
*/
function synonyms_uninstall() {
// Cleaning all configure variables.
$results = db_select('variable', 'var')
->fields('var', array(
'name',
))
->condition('var.name', db_like('synonyms_') . '%', 'LIKE')
->execute();
foreach ($results as $var) {
variable_del($var->name);
}
}
/**
* Implements hook_requirements().
*/
function synonyms_requirements($phase) {
$requirements = array();
switch ($phase) {
case 'update':
// Synonyms has changed its dependencies. It used to be Taxonomy and now
// it is Entity module. We make sure Entity is enabled otherwise we halt
// the update for Synonyms.
// TODO: remove this requirement check at some point, when it becomes
// obsolete.
$t = get_t();
$requirements['synonyms_entity_dependency'] = array(
'title' => $t('Synonyms depends on Entity'),
'description' => $t('Synonyms module depends on Entity module since 4-Mar-2016. At the same time it no longer depends on Taxonomy module while does integrate with the latter. If you do not have installed/enabled Entity module, do so before running updates on Synonyms module. Until Entity module is enabled and updates are executed, Synonyms module performance may degrade and even trigger PHP errors. See <a href="@url">@url</a> for more info.', array(
'@url' => 'https://www.drupal.org/node/1194802',
)),
'value' => module_exists('entity') ? $t('Passed') : $t('Failed'),
'severity' => module_exists('entity') ? REQUIREMENT_INFO : REQUIREMENT_ERROR,
);
break;
}
return $requirements;
}
/**
* Implements hook_update_N().
*
* Update to version 7.x-1.1 of Synonyms module.
*/
function synonyms_update_7101() {
$result = db_select('variable', 'var')
->fields('var', array(
'name',
))
->condition('var.name', db_like('synonyms_settings_') . '%', 'LIKE')
->execute();
foreach ($result as $var) {
$settings = variable_get($var->name);
// Term merging has been deprecated in favor of Term Merge module.
unset($settings['term_merge']);
// Enabled synonyms now stored as field names, since the field independency
// has been introduced. See issue http://drupal.org/node/1850748.
drupal_load('module', 'synonyms');
$settings['synonyms'] = $settings['synonyms'] ? array(
'synonyms_synonyms',
) : array();
variable_set($var->name, $settings);
}
return t('Updated settings of synonyms.');
}
/**
* Multiple adjustments in the internal structures of the module.
*
* Unlock the 'synonyms_synonyms' field, because Synonyms module no longer uses
* it.
* Create 'synonyms_settings' table.
* Enable 'synonyms_search' module if the core Search module is enabled.
* Enable all available behaviors on all "source of synonyms" fields with the
* default settings.
*/
function synonyms_update_7102() {
$field = field_info_field('synonyms_synonyms');
if ($field !== NULL) {
$field['locked'] = FALSE;
field_update_field($field);
}
db_create_table('synonyms_settings', array(
'description' => 'Stores synonyms settings for all the entities and fields. Only enabled synonyms behaviors are included in this table.',
'fields' => array(
'instance_id' => array(
'description' => 'Reference to {field_config_instance}.id of the instance, whose synonyms settings are stored in this row.',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'behavior' => array(
'description' => 'Name of the synonyms behavior (ctools plugin), whose settings are stored in this row.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'settings_serialized' => array(
'description' => 'Settings of the specified synonyms behavior for the specified field instance.',
'type' => 'text',
'serialize' => TRUE,
'not null' => TRUE,
),
),
'unique keys' => array(
'instance_behavior' => array(
'instance_id',
'behavior',
),
),
'foreign keys' => array(
'field_config_instance' => array(
'table' => 'field_config_instance',
'columns' => array(
'instance_id' => 'id',
),
),
),
'indexes' => array(
'behavior' => array(
'behavior',
),
),
));
if (module_exists('search')) {
module_enable(array(
'synonyms_search',
));
}
$vars = db_select('variable', 'v')
->fields('v', array(
'name',
))
->condition('name', db_like('synonyms_settings_') . '%', 'LIKE')
->execute();
foreach ($vars as $row) {
$var = variable_get($row->name);
$vid = substr($row->name, drupal_strlen('synonyms_settings_'));
$vocabulary = taxonomy_vocabulary_load($vid);
if ($vocabulary) {
$bundle = $vocabulary->machine_name;
foreach ($var['synonyms'] as $field) {
$instance = field_info_instance('taxonomy_term', $field, $bundle);
foreach (synonyms_behaviors() as $behavior) {
switch ($behavior['name']) {
case 'synonyms':
case 'search':
default:
$settings = array();
break;
case 'select':
$settings = array(
'wording' => '@synonym',
);
break;
case 'autocomplete':
$settings = array(
'wording' => '@synonym, synonym of @term',
);
break;
}
$settings = array(
'instance_id' => $instance['id'],
'behavior' => $behavior['name'],
'settings' => $settings,
);
synonyms_behavior_settings_save($settings);
}
}
}
variable_del($row->name);
}
}
/**
* Expanding Synonyms module to work with all entity types.
*
* Namely, the following happens:
* - general "synonyms" behavior gets removed as too ambiguous.
* - alter schema of synonyms table
* - "select" and "autocomplete" behaviors undergo change in settings ("@term"
* placeholder is replaced with "@entity" one)
* - taxonomy term reference widgets get renamed to their new names
* - all enabled behavior implementations are renamed to their new names in
* order to support field- and property-based synonyms implementations
*/
function synonyms_update_7103() {
db_delete('synonyms_settings')
->condition('behavior', 'synonyms')
->execute();
db_drop_index('synonyms_settings', 'behavior');
db_drop_unique_key('synonyms_settings', 'instance_behavior');
db_add_field('synonyms_settings', 'entity_type', array(
'description' => 'Entity type whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
));
db_add_field('synonyms_settings', 'bundle', array(
'description' => 'Bundle name whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
));
db_add_field('synonyms_settings', 'provider', array(
'description' => 'Provider name whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
));
db_query('UPDATE {synonyms_settings} s INNER JOIN {field_config_instance} i ON i.id = s.instance_id SET s.entity_type = i.entity_type, s.bundle = i.bundle, s.provider = i.field_name');
db_drop_field('synonyms_settings', 'instance_id');
db_add_unique_key('synonyms_settings', 'behavior_implementation', array(
'behavior',
'entity_type',
'bundle',
'provider',
));
db_add_unique_key('synonyms_settings', 'all_enabled', array(
'entity_type',
'bundle',
'provider',
'behavior',
));
module_enable(array(
'synonyms_provider_field',
));
$alter_behaviors = array(
'select',
'autocomplete',
);
foreach (synonyms_behavior_get_all_enabled() as $behavior_implementation) {
db_delete('synonyms_settings')
->condition('provider', $behavior_implementation['provider'])
->execute();
$behavior_implementation['provider'] = synonyms_provider_field_provider_name(field_info_field($behavior_implementation['provider']));
if (in_array($behavior_implementation['behavior'], $alter_behaviors)) {
$behavior_implementation['settings']['wording'] = str_replace('@term', '@entity', $behavior_implementation['settings']['wording']);
}
synonyms_behavior_implementation_save($behavior_implementation);
}
// Keys are the old widget names, whereas corresponding values are the new
// widget names.
$migrate_map = array(
'synonyms_select' => 'synonyms_select_taxonomy_term',
'synonyms_autocomplete' => 'synonyms_autocomplete_taxonomy_term',
);
foreach (field_info_field_map() as $field_name => $field_info) {
if ($field_info['type'] == 'taxonomy_term_reference') {
foreach ($field_info['bundles'] as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$instance = field_read_instance($entity_type, $field_name, $bundle);
if (in_array($instance['widget']['type'], array_keys($migrate_map))) {
$instance['widget']['type'] = $migrate_map[$instance['widget']['type']];
if ($instance['widget']['type'] == 'synonyms_autocomplete_taxonomy_term' && $instance['widget']['settings']['synonyms_autocomplete_path'] == 'synonyms/autocomplete') {
$instance['widget']['settings']['synonyms_autocomplete_path'] = 'synonyms/autocomplete-taxonomy-term';
}
field_update_instance($instance);
}
}
}
}
}
}
/**
* Truncating 'entity_type' and 'bundle' columns to 128 length.
*/
function synonyms_update_7104() {
$table = 'synonyms_settings';
$indexes = array(
'unique keys' => array(
// We build 2 different indexes on the same column set because there are
// 2 different functions that may query this table and the columns they
// filter on may vary.
'behavior_implementation' => array(
'behavior',
'entity_type',
'bundle',
'provider',
),
'all_enabled' => array(
'entity_type',
'bundle',
'provider',
'behavior',
),
),
);
$fields = array(
'entity_type' => array(
'description' => 'Entity type whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'bundle' => array(
'description' => 'Bundle name whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
);
foreach ($fields as $field => $schema) {
foreach ($indexes['unique keys'] as $index_name => $index_specification) {
db_drop_unique_key($table, $index_name);
}
db_change_field($table, $field, $field, $schema, $indexes);
}
}
/**
* Making actual module tables follow the declared schema.
*/
function synonyms_update_7105() {
db_drop_unique_key('synonyms_settings', 'behavior_implementation');
db_drop_unique_key('synonyms_settings', 'all_enabled');
db_change_field('synonyms_settings', 'provider', 'provider', array(
'description' => 'Provider name whose behavior implementation is stored in this row.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
), array(
'unique keys' => array(
'behavior_implementation' => array(
'behavior',
'entity_type',
'bundle',
'provider',
),
'all_enabled' => array(
'entity_type',
'bundle',
'provider',
'behavior',
),
),
));
}
Functions
Name | Description |
---|---|
synonyms_requirements | Implements hook_requirements(). |
synonyms_schema | Implements hook_schema(). |
synonyms_uninstall | Implements hook_uninstall(). |
synonyms_update_7101 | Implements hook_update_N(). |
synonyms_update_7102 | Multiple adjustments in the internal structures of the module. |
synonyms_update_7103 | Expanding Synonyms module to work with all entity types. |
synonyms_update_7104 | Truncating 'entity_type' and 'bundle' columns to 128 length. |
synonyms_update_7105 | Making actual module tables follow the declared schema. |