Time Saving Tips for Drupal Buildouts

Automation with make files and installation profiles

July 23, 2014
Development, Footer, Drupal


New Media Campaigns builds a lot of Drupal sites. And we've learned the hard way that it is very easy to waste valuable time during the Drupal installation process. For our first few Drupal sites, we would download the .zip archive, extract it, and follow the installation screens at /install.php in the web browser. After the core system was installed, we'd download our modules, extract them into sites/all/modules/, and activate them in the Drupal control panel. Don't do this.

When you build a Drupal site, your objective is to create a website that your client can edit. Don't waste time during the installation process. Installation is an implementation detail that can (and should) be abstracted away with automation.

Fortunately, Drush (which stands for Drupal shell) lets us automate pretty much anything Drupal-related — even installation.

Make files

Drush provides a make command that can download the core software and contrib modules you'll need to build a Drupal site. All you have to do is write a simple manifest file. You can name this text file anything you like, but I recommend you use a .make file extension.

The make manifest file lists all core and contrib software that you want to use for a given Drupal website buildout. The manifest is a plain text file that adheres to the Drupal .info file format. The manifest always begins with the core Drupal software version that you want to use:

core = "7.x" 

You must also specify the Drush make API version to use:

api = "2" 

Next, you provide an array of projects (core software, libraries, and modules) to download. You can also specify certain properties for each project, including a specific version number, patch number, destination directory, and type (core, module, profile or theme). Here's an example that tells our make manifest to download the Views module (version 3) into the sites/all/modules/contrib/ directory.

projects[views][type] = "module" 
projects[views][version] = "3" 
projects[views][subdir] = "contrib" 

Some Drupal sites also rely on libraries like jQuery or FullCalendar. Because these are not Drupal projects, we have to specify those a bit differently in our make manfiest. This example demonstrates how to download the jQuery library:

libraries[jquery][download][type] = "file" 
libraries[jquery][download][url] = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js" 
libraries[jquery][destination] = "libraries" 

Once you have prepared your make manifest, you can use it to download all of the software required for your Drupal buildout with a single command:

drush make /path/to/manifest.make /path/to/install-dir 

After you execute this command, you should have all the software you need in the /path/to/install-dir directory. Now you'll need to run the Drupal installer. You could of course do this in your web browser and then manually activate your modules. Don't. Remember, we're all about automation here. Let's use Drush to perform a site install with a custom installation profile.

Install profiles

A Drupal installation profile tells Drupal what functionality and modules to enable during the installation process. Typically, your installation profile will enable the same modules and libraries that are listed in your Drush make manifest. The installation profile contains two plain text files: my-profile.info and my-profile.install. Both files should use the same name (excluding their file extensions). The name of these files becomes the custom installation profile name that is referenced later.

The .info file

The .info file describes your installation profile and lists which dependencies (i.e. modules) to enable during installation. First, you define the profile name and description:

name = "My Installation Profile" 
description = "This is my custom installation profile" 

Then you specify the Drupal core version:

core = "7.x" 

Then you define all of the dependencies (i.e. modules) that should be enabled during installation:

dependencies[] = "block" 
dependencies[] = "comment" 
dependencies[] = "contextual" 
dependencies[] = "dashboard" 
# ... and so on ... 

You can also pre-define Drupal variables in the .info file, too. This lets you save even more time by pre-configuring your contrib modules (e.g. jQuery Update, SMTP, or IMCE) automatically with your preferred settings.

variables[admin_theme] = 'seven' 

The .install file

The .install file is a PHP file that implements the hook_install Drupal hook. This is invoked during installation so that your custom profile will be called during the Drupal installation process. The .install file will look something like this:

function profilename_install() { 
    // Do something 

Inside this PHP function, you can use the Drupal API to perform whatever post-installation tasks you need, such as creating users, roles, and blocks.

Profile Builder

Because we are all about automation, you can create a custom installation profile based on an existing Drupal installation with the Profile Builder module. Simply install this module on an existing site, and use its user interface in the Drupal control panel to create and download a custom installation profile for future Drupal buildouts. Learn more at https://www.drupal.org/project/profiler_builder.

Using your installation profile

Here's how you use your custom installation profile. You'll want to create a new subdirectory in the profiles/ directory; the profiles/ directory is inside the Drupal root directory. Move your .info and .install files into your new custom profile directory. Next, execute this command from inside the Drupal root directory.

drush site-install your-profile-name 
    --site-name="My Site" 

Replace "your-profile-name" with the name of your custom installation profile, and swap in your own database and account details. This will perform the complete Drupal website installation via the command line using the profile, database, and user account information specified with the additional command flags. When this command finishes, you can navigate to /user/login in your browser and log in.

Final process

This is a lot info, so allow me to summarize. When you are ready to create a new Drupal site, start with your Drush .make file. Use this to download all of the core and contrib software to your document root directory. Next, you will use your custom installation profile to perform the actual site installation and run any post-installation tasks. And now you're ready to start the Drupal site building process!

Drupal installation is a tedious chore that can easily be abstracted away with custom a .make file and a custom installation profile. Once you've found a .make file and a custom installation profile that you like, you can version control these so you can pull them down for your future Drupal buildouts.

Leave the first comment