[Drupal] How to Create Deploy Module in Drupal 7 ?

Zyxware default image4

Most of the operations that are done when developing sites in Drupal deals with enabling and configuring modules that are to be saved for future use. This is most certain times a time consuming process. But what if we could automate these tasks by developing a deployment module. Let's find out how we could achieve this goal.

The technique of deployment module is pretty straight forward and easily understandable. We should create one custom module which handles all configurations in our site. The configurations will be written inside the .install file of the custom module. Finally implement hook_update_N(). Typically this hook can be implemented as one hook_update_N() for each release. The deploy module can be executed by running update.php file, or you can use command drush updb (if Drush is installed).

The hook_update_N() is used when update in database is needed. For the hook 'N' starts from the number 7000 each time newer upgrade is required use the number incremented by 1 (i.e, 7001, 7002 and so on). Lets check how to implement one deploy module.

First create a module skeleton for 'custom_deploy' module and add files 'custom_deploy.info, custom_deploy.install, custom_deploy.module'.

In custom_deploy.install file, implement hook_update_N() with configurations to be done. In this case I'm adding code to enable 'Field Collection and Chosen' modules.


  /**
   * Implements hook_update_N().
   *
   * Enables the modules.
   */
  function custom_deploy_update_7001() {
    // List of the modules which needs to be enabled initially.
    $contrib_modules_to_enable = array(
      'field_collection',
      'chosen',
    );
    module_enable($contrib_modules_to_enable, TRUE);
  }

Above code implements hook_update_N() as custom_deploy_update_7001(), the updated uses Drupal's module_enable() function to enable the modules. If multiple updates are required we could loop the updates inside one hook_install() hook. For example, we need to add one more update to enable 'rules' module so we implements custom_deploy_update_7002().


  /**
   * Implements hook_update_N().
   *
   * Enables the modules.
   */
  function custom_deploy_update_7002() {
    // List of the modules which needs to be enabled initially.
    $contrib_modules_to_enable = array(
      'rules',
    );
    module_enable($contrib_modules_to_enable, TRUE);
  }

Next implement hook_install() to install our updates. This hook is used to implement set-up tasks when a module is installed. If there are multiple updates we could iterate through them like as follows:


  /**
   * Implements hook_install().
   */
  function custom_deploy_install() {
    // Looping through the updates and executing them.
    // Change the update number accordingly when more updates are
    // written.
    for ($i = 7001; $i < 7003; $i++) {
      $candidate = 'custom_deploy_update_' . $i;
      if (function_exists($candidate)) {
        $candidate();
      }
    }
  }

In the above example update count will iterate each time resulting each time calling the update function.

Please feel to comment and share. Also, please feel free to get in touch with us if any queries.