11 Useful Functions for Managing Your Drupal 7 Configuration in Code

| | 2 min read

In Drupal 8 or above managing configuration in code is not a challenge, but in the case of Drupal 7, it is still a big issue. Developers often create a module for this and use hook_update to maintain configuration in code. Here we are trying to list 10 common use cases for the making configuration changes using hook_update in Drupal 71 .

1. Disable and Uninstall a module

function zyxconf_update_2000() {
  module_disable(array('module1', 'module2'));
  drupal_uninstall_modules(array('module1', 'module2'));
}

 

 

2. Delete role

function zyxconf_update_2000() {
  user_role_delete('Dept Reviewer');
}

3. Install module

function zyxconf_update_2000() {
  module_enable(array('module1', 'module2'));
}

4. Add role visibility to a block

function zyxconf_update_2000() {
  $module = 'module1';
  $delta = 'delta';
  $role = user_role_load_by_name('role 1');
  db_insert('block_role')
    ->fields(array('rid', 'module', 'delta'))
    ->values(array('rid' => $role->rid, 'module' => $module, 'delta' => $delta))
    ->execute();
}

 

 

5. Delete table

function zyxconf_update_2000() {
  db_delete('table_name')
    ->execute();
}

6. Disable rule

function zyxconf_update_2000() {
  if ($rules_config = rules_config_load('rules_lth_configuration_rule1')) {
    $rules_config->active = FALSE;
    $rules_config->save();
  }
}

7. Revert rule

function zyxconf_update_2000() {
  if ($rules_config = rules_config_load('rules_zyxconf_rule1')) {
    $rules_config->delete();
  }
}

8. Revert feature

function zyxconf_update_2000() {
  features_revert(array('zyx_features' => array('features_component')));
}

9. Delete multiple custom blocks

function zyxconf_update_2000() {
  $module = 'block';
  $deltas = array('24', '25');
  foreach ($deltas as $delta) {
    db_delete('block')
      ->condition('module', $module)
      ->condition('delta', $delta)
      ->execute();
    db_delete('block_custom')
      ->condition('bid', $delta)
      ->execute();
    db_delete('block_role')
      ->condition('module', $module)
      ->condition('delta', $delta)
      ->execute();
  }
}

10. Add permissions

function zyxconf_update_2000() {
  user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('permission name')); // Anonymous users
  user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, array('permission name')); // Authentificated users
  user_role_grant_permissions(3, array('permission name')); // Role ID
}

11. Deletes nodes and content types

function zyxconf_update_2000() {
  $types = array(
    'forum',
    'poll',
  );
  foreach($types as $type) {
    // First, delete nodes
    $results = db_select('node', 'n')
                ->fields('n', array('nid'))
                ->condition('type', $type)
                ->execute();
    foreach ($results as $result) {
      $nids[] = $result->nid;
    }
    if (!empty($nids)) {
      node_delete_multiple($nids);
      drupal_set_message(t('%count nodes has been deleted.', array('%count' => count($nids))));
    }
    // Then, delete content type
    if (node_type_load($type)) {
      node_type_delete($type);
      variable_del('node_preview_' . $type);
      drupal_set_message(t('%type content type has been deleted.', array('%type' => $type)));
    }
  }
  node_types_rebuild();
  variable_set('menu_rebuild_needed', TRUE);
}

Thank you Adarsh M for sharing this collection