I've spent some time recently building a Cameron & Wilding Drupal 7 installation profile. Whenever we set up a new site from scratch, be it for testing or development on a new project, we always end up installing a host of useful modules: Devel, Views, CTools and Features, to name just a few of them. I was inspired by Antonio and Andrea's great presentation at DrupalCon London last month. They go into detail about maintaining a Drupal distribution with the Features module. This helps you keep all your configuration in code and means you can do really quick, clean deployments.
I'll let you look at the DrupalCon presentation to discover the full potential that this method holds, because in this post I'm going to look at creating a basic install profile with a Drush make file, a git repository and a minimum of coding. I've also borrowed from the documentation for Drupal 7 Install Profiles.
One of the great things about Drush makefiles is that it's possible to put together a complicated module or profile with just a single Drush command. This has been especially useful in the office because I can share the C&W installation profile with the rest of the team just by emailing them a single make file which they run to install Drupal. The installation profile itself is still a work in progress and we're deciding exactly which modules to include in it. It will no doubt evolve over time, so for the purposes of this post we'll use an example profile (that I've put in a Git repository on github) and I'll explain how to customise it for yourself. Wondering why you should use Git?
The new installation profile will provide an additional installation option when you set up your Drupal 7 site. When we've finished, you should be able to choose from a list of options like this:

So, here's the contents of the makefile:
core = 7.x
api = 2
projects[drupal][version] = "7.8"
projects[candw][type] = "profile"
projects[candw][download][type] = "git"
projects[candw][download][url] = "https://github.com/edwardcrompton/candw.git"
projects[candw][download][branch] = "master"
Yep, it's just 7 lines. In fact this is going to be the first of two makefiles that we'll use, but this is the one that we'll run with drush to build the site. Essentially, the make file contains two things: The version of Drupal core that we're going to use and a reference to the github repository where I've put my installation profile files. You can find more details on the structure of a Drush makefile here.
You should be able to run this makefile with Drush. To do so, you'll need to install Drush (but you've already seen the light and started using Drush, right?) and Drush Make. There are various tutorials for installing both these things, so I won't go into that here. It's worth noting though that although the current version of Drush Make at the time of writing is 6.x-2.3, it also works with Drupal 7.
I'm going to assume in this example that you're doing your Drupal installation on some kind of Linux server. I'm using Ubuntu 10.04 here.
To run the makefile, save the example above as candw-stub.make. We'll put our Drupal installation in a folder called candwexample in our web document root (on Ubuntu this is at /var/www). Change director to your web root and then run this command:
drush make candw-stub.make candwexample
You should see something like this:
Project information for drupal retrieved.
drupal downloaded from http://ftp.drupal.org/files/projects/drupal-7.8.tar.gz.
candw cloned from [email protected]:edwardcrompton/candw.git.
Found makefile: candw.make
Project information for views retrieved.
Project information for ctools retrieved.
views downloaded from http://ftp.drupal.org/files/projects/views-7.x-3.0-rc1.tar.gz.
ctools downloaded from http://ftp.drupal.org/files/projects/ctools-7.x-1.0-rc1.tar.gz.
high_contrast cloned from http://git.drupal.org/sandbox/acontia/1281604.git
This will download everything you need for your new Drupal 7 site. You'll notice that we've downloaded Views, CTools and another module called high_contrast. This is a module my colleague Pablo built the other day and I thought I'd use it as a good example of how a custom module can be fetched from a Git repository. The module is in a sandbox on drupal.org, but could just as easily be in a repository on github or elsewhere.
Let's take a look at the structure of the Drupal filesystem that we've just set up. The installation profiles in Drupal 7 are contained in the profiles folder and each one contains any modules that it requires. If you look in the profiles directory inside the candwexample module you'll notice that the Views and CTools modules are downloaded to the path candwexample/profiles/candw/modules/contrib. If you poke about a bit further you will discover that our custom high_contract module has also been downloaded to candwexample/profiles/candw/modules/custom/high_contrast.
But how did Drush know to download these extra modules? Views and CTools are not part of Drupal 7 core and we didn't mention them in our makefile. Neither did we make any reference to the high_contrast module. Well, look back at one of the lines that drush outputted when you ran the makefile:
Found makefile: candw.make
Drush has cloned the profile that we pointed it to on github and it has found another make file in there, which it has also run. Let's have a closer look at the files in the git repository. Drush will keep running makefiles recursively for any of the stuff that it downloads or clones from a repository as a result of the first make file we ran from the command line. In this example we will only come across two makefiles: The first 'stub' (candw-stub.make) that we just ran and the second (candw.make) that is in our custom profile. The stub makefile is kept as simple as possible and it does not change very often. Because it does not change very often we don't need to keep it under version control.
The make file in the profile is part of our git repository and it will change whenever we decide to add or remove a module from our custom Drupal profile or we want to change the version of a module we're using. In this example the profile is in a repository on github. Github is free to sign up for and I'd recommend that you set up an account and put your own custom installation profile in a repository there. As long as you change your makefile to include the path to your repository Drush should always be able to find your installation profile wherever you are installing Drupal.
This is what the makefile in our profile looks like:
core = 7.x
api = 2
; Contrib
projects[views][subdir] = "contrib"
projects[views][version] = "3.0-rc1"
projects[ctools][subdir] = "contrib"
projects[ctools][version] = "1.0-rc1"
; Custom
projects[high_contrast][type] = "module"
projects[high_contrast][download][type] = "git"
projects[high_contrast][download][url] = "http://git.drupal.org/sandbox/acontia/1281604.git"
projects[high_contrast][subdir] = "custom"
In this makefile we're providing details of the three modules we want to download so that they can be used in our profile. Each line in the makefile here (as with the first makefile) is of the format project[name_of_module][some_parameter]. For example, project[views][version] = "3.0-rc1" tells Drush to get the 7.x-3.0-rc1 version of the Views module and projects[views][subdir] = "contrib" tells Drush to download it to a path at candwexample/profiles/candw/modules/contrib. (Without this line it would by default download to candwexample/profiles/candw/modules). Because Views and CTools are contributed modules Drush knows where to download them from automatically.
As you can see, there are extra parameters for our custom high_contrast module. Amongst other things Drush needs to know the location of the repository to get it from and the type of that repository (Git on drupal.org in this case, but it could also be an svn repository).
Drupal.org has full documentation on the makefile format.
The other really important file in the repository (https://github.com/edwardcrompton/candw) is candw.info. This sets the modules on which the profile depends. It is very similar to the .info file you find in every Drupal module. You might think that the modules your profile is dependent on will always be the same as the modules that we've specified in our makefile, right? Not necessarily - the modules listed as dependancies in the candw.info file will be automatically enabled when the installation profile is run. You may not immediately want to enable all the modules that you have downloaded with the Drush makefile, so these would not be included the .info file. Additionally, there may be optional core modules that you want to enable but which you don't need to explicitly mention in your Drush makefile because they are included as part of Drupal core. We will include these in the .info file.
This is what the candw.info file looks like in our example:
name = C&W Blog
description = Install as a Cameron & Wilding blog site.
version = 1.0
core = 7.x
;Standard install core modules
dependencies[] = block
dependencies[] = color
dependencies[] = comment
dependencies[] = contextual
dependencies[] = dashboard
dependencies[] = help
dependencies[] = image
dependencies[] = list
dependencies[] = menu
dependencies[] = number
dependencies[] = options
dependencies[] = path
dependencies[] = taxonomy
dependencies[] = dblog
dependencies[] = search
dependencies[] = shortcut
dependencies[] = toolbar
dependencies[] = overlay
dependencies[] = field_ui
dependencies[] = file
dependencies[] = rdf
;Contrib modules for C&W
dependencies[] = views
;Custom modules for C&W
dependencies[] = high_contrast
The first four lines specify some general details about our profile: The name and description of the installation profile as well as the version of the module and the version of Drupal core we will be using. The name and description will show up when you run your custom Drupal installation profile when installing Drupal.
The 20 or so lines under the 'Standard install core modules' heading is a list of the core modules that we want to enable. These are the modules that Drupal enables by default when you do a standard installation. The last two lines in the file tell Drush to enable Views and our high_contrast module. You'll notice that CTools is not listed as a dependancy. This is because it is a dependancy of Views so it will automatically be enabled when Views is enabled.
Apart from the README.txt file in the github repository, there are two other files that we haven't yet looked at: candw.install and candw.profile. These are very similar to the mymodule.install file and mymodule.module file that you find in a Drupal module. candw.profile can contain PHP code that will dictate the behaviour of your install profile. You can use it to set variables, create user roles and also add extra steps to your Drupal installation process. For the purposes of this example install profile we are going to leave the candw.profile file blank. (I did say this would be a simple example!) There are a few really useful hooks that you can put in this file.
In the candw.install file we'll just place a single hook with a few lines of code:
/**
* Implements hook_install().
*
* Perform actions to set up the site for this profile.
* We'll just call the install method from the standard install here.
* Everything else we do will be on top of that.
*/
function candw_install() {
include_once DRUPAL_ROOT . '/profiles/standard/standard.install';
standard_install();
}
Hook_install runs when the installation profile is installed. You'll know all about this hook if you've ever done any module development. In a module it's where you do all your initial configuration and a profile is no different. We're going to cheat a bit here and use some code that's already in Drupal core - we'll simply call the hook_install from the standard Drupal installation. This does some of the stuff that you take for granted when you install Drupal. Amongst many other things it sets up some basic blocks (for example login and search) and content types (Basic Page and Article).
Now we have a working Drupal 7 install profile which can be extended to include any number of custom or contributed modules. Do take a look at the DrupalCon presentation I mentioned at the start to see how powerful these basic concepts become when you start incorporating Features modules as part of your install profile. Using Features you can create install profiles that contain all your configuration so you can set up Views, new content types or taxonomy vocabularies to name only a few of the possibilities.
If you've run your Drush makefile in your document root to download to a folder called candwexample you should now be able to navigate to http://localhost/candwexample and choose your new custom installation profile from the list as shown in the screenshot at the top of this post.


Add new comment