5 * Example "Sync enable" sql-sync command alter.
7 * For Drupal 8, please use the config_split module instead of this code. Drupal
8 * 6 and 7 sites may use this example.
10 * Sync_enable adds options to sql-sync to enable and disable
11 * modules after an sql-sync operation. One use case for this
12 * is to use Drush site aliases to automatically enable your
13 * development modules whenever you sync from your live site to
14 * your dev site. You may also add or remove permissions at
20 * $aliases['dev'] = array (
21 * 'root' => '/srv/www/drupal',
22 * 'uri' => 'site.com',
23 * 'target-command-specific' => array(
24 * 'sql-sync' => array(
25 * 'enable' => array('devel', 'hacked'),
26 * 'disable' => array('securepages'),
27 * 'permission' => array(
28 * 'authenticated user' => array(
29 * 'add' => array('access devel information', 'access environment indicator'),
30 * 'remove' => 'change own password',
32 * 'anonymous user' => array(
33 * 'add' => 'access environment indicator',
41 * To use this feature, copy the 'target-command-specific'
42 * item from the example alias above, place it in your development
43 * site aliases, and customize the development module list
44 * to suit. You must also copy the sync_enable.drush.inc
45 * file to a location where Drush will find it, such as
46 * $HOME/.drush. See `drush topic docs-commands` for more
49 * To set variables on a development site:
51 * Instead of calling variable_set and variable_delete in a post-sync
52 * hook, consider adding $conf variables to settings.php.
56 * $conf['error_level'] = 2;
57 * error_reporting(E_ALL);
58 * ini_set('display_errors', TRUE);
59 * ini_set('display_startup_errors', TRUE);
60 * $conf['preprocess_css'] = 0;
62 * $conf['googleanalytics_account'] = '';
66 * Implements hook_drush_help_alter().
68 * When a hook extends a command with additional options, it must
69 * implement help alter and declare the option(s). Doing so will add
70 * the option to the help text for the modified command, and will also
71 * allow the new option to be specified on the command line. Without
72 * this, Drush will fail with an error when a user attempts to use
75 function sync_enable_drush_help_alter(&$command) {
76 if ($command['command'] == 'sql-sync') {
77 $command['options']['updb'] = "Apply database updates on the target database after the sync operation has completed.";
78 $command['options']['enable'] = "Enable the specified modules in the target database after the sync operation has completed.";
79 $command['options']['disable'] = "Disable the specified modules in the target database after the sync operation has completed.";
80 $command['options']['permission'] = "Add or remove permissions from a role in the target database after the sync operation has completed. The value of this option must be an array, so it may only be specified in a site alias record or drush configuration file. See `drush topic docs-example-sync-extension`.";
85 * Implements drush_hook_post_COMMAND().
87 * The post hook is only called if the sql-sync operation completes
88 * without an error. When called, we check to see if the user specified
89 * any modules to enable/disable. If so, we will call pm-enable/pm-disable on
92 function drush_sync_enable_post_sql_sync($source = NULL, $destination = NULL) {
93 $updb = drush_get_option('updb', FALSE);
95 drush_log('Run database updates', 'ok');
96 drush_invoke_process($destination, 'updatedb', array(), array('yes' => TRUE));
98 $modules_to_enable = drush_get_option_list('enable');
99 if (!empty($modules_to_enable)) {
100 drush_log(dt("Enable !modules post-sql-sync", array('!modules' => implode(',', $modules_to_enable))), 'ok');
101 drush_invoke_process($destination, 'pm-enable', $modules_to_enable, array('yes' => TRUE));
103 $modules_to_disable = drush_get_option_list('disable');
104 if (!empty($modules_to_disable)) {
105 drush_log(dt("Disable !modules post-sql-sync", array('!modules' => implode(',', $modules_to_disable))), 'ok');
106 drush_invoke_process($destination, 'pm-disable', $modules_to_disable, array('yes' => TRUE));
108 $permissions_table = drush_get_option('permission');
109 if (!empty($permissions_table)) {
110 foreach ($permissions_table as $role_name => $actions) {
111 if (array_key_exists('add', $actions)) {
112 $permissions_to_add = is_array($actions['add']) ? $actions['add'] : explode(', ', $actions['add']);
113 foreach ($permissions_to_add as $permission) {
114 $values = drush_invoke_process($destination, 'role-add-perm', array($role_name, $permission), array(), array('integrate' => TRUE));
117 if (array_key_exists('remove', $actions)) {
118 $permissions_to_remove = is_array($actions['remove']) ? $actions['remove'] : explode(', ', $actions['remove']);
119 foreach ($permissions_to_remove as $permission) {
120 $values = drush_invoke_process($destination, 'role-remove-perm', array($role_name, $permission), array(), array('integrate' => TRUE));