Skip to content

Instantly share code, notes, and snippets.

@juampynr
Last active January 3, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juampynr/8434983 to your computer and use it in GitHub Desktop.
Save juampynr/8434983 to your computer and use it in GitHub Desktop.
Drupal 7: Common use cases on database updates.
<?php
/**
* Disable and uninstall a feature module.
*
* Note: see some edge cases where some exported components like content types
* need to be removed manually.
*/
function mymodule_update_7100() {
// Note that this does not take care of disabling modules that depend
// on this one.
module_disable(array('some_module'));
drupal_uninstall_modules(array('some_module));
}
/**
* Delete a field definition and all of its instances.
*/
function mymodule_update_7100(&$sandbox) {
field_delete_field('field_name');
field_purge_batch(1000);
}
/**
* Delete a content type and taxonomy contained in a feature before disabling it.
*
* Based on http://drupal.stackexchange.com/questions/44547/unable-to-delete-feature-content-type-after-uninstalling-feature/.
*/
function mymodule_update_7100(&$sandbox) {
$content_type = 'content_type_to_delete';
$module = 'feature_module_where_its_exported';
// Make content type's fields deletable.
db_query("UPDATE {node_type} SET custom = 1, locked = 0 WHERE type = :ctype", array(':ctype' => $content_type));
// Clean Drupal cache; this is necessary for the content type too be 'deletable'.
drupal_flush_all_caches();
node_type_delete($content_type);
module_disable(array($module));
drupal_uninstall_modules(array($module));
// Delete taxonomies.
$machine_names = array('exported_taxonomy1', 'exported_taxonomy2');
foreach ($machine_names as $machine_name) {
$taxonomy = taxonomy_vocabulary_machine_name_load($machine_name);
taxonomy_vocabulary_delete($taxonomy->vid);
}
}
/**
* Drops indexes from field collection tables which are not at {field_config}
*
* This lets Features to recreate them and update {field_config} table
* once database updates have completed and features are reverted.
*/
function mymodule_update_7100() {
db_drop_index('ffield_datos_contacto', 'field_datos_contacto_revision_id');
}
/**
* Adds a new checkbox field and sets existing nodes to default to 0.
*
* This is useful specially for Views and EFQ since even though you may
* have set the default value to be 0, existing data will have a value of
* NULL.
*/
function mymodule_update_7105() {
// We need to revert the feature components first, to be sure the field exists
// so that data can be saved to it.
$items['mymodule'] = array('field_base', 'field_instance');
features_revert($items);
// Load up all show nodes.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'mycontenttype');
$results = $query->execute();
// If we don't have any results, throw an exception. The only reason we
// wouldn't get any results is if something went wrong.
if (!isset($results['node']) || empty($results['node'])) {
$t_args = array('@function' => __FUNCTION__);
throw new DrupalUpdateException(t('Unable to find mycontenttype nodes in update @function.', $t_args));
}
// Loop over each node and set field_myfield to 0.
foreach (node_load_multiple(array_keys($results['node'])) as $node) {
$node->field_myfield[LANGUAGE_NONE][0]['value'] = 0;
node_save($node);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment