A Slideshow Feature - part 2

In the first part I've shown a way to create a simple slideshow in Drupal. In this part I'm going to show you how to reproduce these steps with a few simple commands.

If you don't have drush installed, you can do it from here.
I'm going to assume that you put your contrib modules into a contrib subfolder (e.g. sites/all/modules/contrib), feature modules will go into a features subfolder (sites/all/modules/features).

1. Create a makefile

If you already have a Drupal installation, go to it's root directory and create a file with the following content:

core = 7.x
api = 2
projects[featuresslideshow][download][type] = "git"
projects[featuresslideshow][download][url] = "https://github.com/progpapa/featuresslideshow"
projects[featuresslideshow][download][branch] = "7.x-2.x"
projects[featuresslideshow][type] = "module"
projects[featuresslideshow][subdir] = "features"
projects[featuresslideshow][directory_name] = "featuresslideshow"

The filename could be whatever you want, let's call it slideshow.make.

2. Run drush make

Now run drush make:

drush make slideshow.make --no-core -y

This is what happens after you hit enter:

  • It goes to my github account and downloads the featuresslideshow module, that we specified in the makefile above.
  • drush make founds that my module also has a makefile: featuresslideshow.make (name of the module + .make), so it reads its content
  • drush downloads the modules that I've defined in featuresslideshow.make
  • drush applies the patch I've defined in the makefile

3. Enable the feature

Now the slideshow feature module and all its dependencies are downloaded, you can enable the module:

drush -y en feautresslideshow

This will enable the feature and all the contrib module it requires.

4. The result

  • Now you should have a content type (called Slide) with title and an image field
  • An image style (called slide) that will be used to display the slideshow
  • A view (called Slideshow) that will display the slideshow
  • A nodequeue (called Slideshow) that will store the slides to display
  • A rule (called Actions after creating new slide node) that will auto-add all new slide content to the queue
  • 2 page manager variants to redirect users trying to access the slide node (anonymous users will be redirected to the front page, users who can edit the slide node will go to the node edit form)

The only thing left is to assign the block display of the Slideshow view to a region. That's it, your slideshow should be working.

5. Takeaways

Besides being able to create a slideshow in 1 minute, there are a few other things worth mentioning.

5.1 Use Strongarm

The Strongarm module allows you to export settings into your features that are stored as variables in the variable database table. It's quite likely that most if not all of your features will need this module. Things like publishing options, comment settings of content types, pathauto patterns and a bunch of other things are stored as variables. Once you have enabled the Strongarm module you'll see the variables you can export in the Strongarm section of the feature creation page.

5.2 Add makefiles to your features

As you have seen, the featuresslideshow.make file makes it really easy to gather all dependencies of the feature. It's a good idea to add such makefiles to your features, this will make your life a lot easier if you are using drush make.

Btw., if you check your libraries directory (usually sites/all/libraries) you'll see that the jquery cycle plugin has been downloaded. There is nothing about this in my makefile, so how was this downloaded?

Well, my makefile tells drush to download the Views Slideshow module and that module also has a makefile which tells drush to download the cycle plugin. This method of chaining makefiles is very powerful and that's why it makes a lot of sense to add makefiles to your feature modules. You can create makefiles for install profiles and if you add a feature you need not worry about what modules the feature depends on as the makefile of the feature will take care of that.

5.3 Upload your feature to GitHub

Uploading your features to GitHub or Bitbucket will make it easy to share your features and add them to makefiles. You've seen in section 1. how to download from GitHub using https, here is another example, which downloads from Bitbucket using SSH:

projects[featuresslideshow][download][type] = "git"
projects[featuresslideshow][download][url] = "git@bitbucket.org:progpapa/featuresslideshow.git"
projects[featuresslideshow][download][branch] = "7.x-2.x"
projects[featuresslideshow][type] = "module"
projects[featuresslideshow][subdir] = "features"
projects[featuresslideshow][directory_name] = "featuresslideshow"

Note: You won't be able to download this feature as this is a private repo.

5.4 Add your own code to the feature

You can add your own code to any feature module, just as you'd do with a regular module.

E.g. you can add a css file to the slideshow feature and add code to the .module file to load the css file.

/**
 * Implements hook_init().
 */
function featuresslideshow_init() {
  drupal_add_css(drupal_get_path('module', 'featuresslideshow') . '/mycssfile.css');
}

I hope you now see the power of using features, drush and makefiles in speeding up sitebuilding. In the next article I'm going to take it one step further and show you how to use this and some more features in an install profile.

Services

Drupal theming and sitebuild

  • PSD to Drupal theme
  • Installing and configuring modules (Views, CCK, Rules, etc.)
  • Theming modules' output

Drupal module development

  • Almost every site needs smaller modules to modify HTML output or override certain default functionalities
  • Developing custom modules

From the blog