fromJune 2014

Drush: The Swiss Army Knife for Drupal

Part Three: The Tweezers

I’m personally amazed at the new features and advances of Drupal 8. There are so many changes to talk about, but for this article I want to cover configuration management. In case you haven't yet heard, with the new version of Drupal we see all configuration stored in files instead of in the database. With the new version of Drush there are some built in tools to help manage these files.

At the time of this writing, the recommended version of Drush is 6; however, this article uses commands that were introduced with version 7. Before you can try any of these, you’ll need to be running that version of Drush. The tricky part is that it currently does not have a full release. Typically, you would not want to install a project’s dev release in a production environment. I’m going to assume you will be working locally, on a development server, or are confident you can’t hurt anything otherwise.

In a previous article I explained how to install Drush using Pear; however, the project is shifting to Composer. You can still install via Pear, or even manually, but it's recommended you switch to Composer. Drush has also been moved to GitHub; you will find additional instructions for installing with something other than composer there.

Assuming your environment already has Composer installed, get the latest release of Drush 7 and issue the below command.

composer global require drush/drush:dev-master --prefer-source

Next, verify that it installed:

drush version

The output should indicate you have Drush 7.0-dev, or something along those lines. If it didn't work you may need to log out and back in again before your SSH user will know it's installed.

Now that we have the latest and greatest, the next thing to do is get Drupal 8. Using Drush you can request a specific version of Drupal, or any project, by providing the version number. If you don’t provide a version it will find the recommended version and download that instead.

To get the dev version of Drupal, type this:

drush dl drupal-8.x-dev

Next, let’s get a site installed. Keep in mind that you will need to configure your web server and database since this command only handles the install of Drupal.

drush site-install --db-url=mysql://USER:PASSWORD@localhost/DATABASE

Upon completion, the username and password will output to your screen.

Checkpoint! Now we can explore the site’s configuration.

Drupal creates a folder with a long randomly-generated name inside the site’s files folder. Inside this folder it writes out every setting to a YAML file. The great thing about this is for those who want to run version control they can now track every change to Drupal's configuration. Previously this was not possible, since it was all inside the database.

Now that we have this folder, the maintainers of Drush have added a new set of commands to help us more easily find, view, and modify these files.

Let's play around with some site settings. Type in the command below to get the active configuration.

drush config-get

The output will show you the site’s name, e-mail, front page, error pages, etc. You will notice that the Site Name is set to something generic: that is a result of installing with Drush but not providing a name at the time of install. Let’s update the configuration file and provide a new name.

drush config-set name “Drush is the Best”

Now check your site. You will see the change reflected immediately.

Using the same command structure, you can modify any configuration: in the above command, “” is the configuration file, “name” is the key, and “Drush is the Best” is the value.
Using config-set works great when modifying a single value, but lets say you’d like to set the site’s name, e-mail, and front page. It would likely be easier to use the following:

drush config-edit

This will open the specified configuration file and allow you to modify the file in the terminal. Save it and the configuration changes are applied.

I’m also a fan of using the shortcuts: instead of typing out config-get and config-set, try using cget and cset instead.

One of the harder things to master is knowing where to find the particular configuration you are looking for. Fortunately there is a command to help us with that. Let's say you want to edit something related to user settings. Since you know it's a user setting, you can list all the user files by issuing this command:

drush config-list user

The output will list every file available in user settings, which allows you to find the file you wish and modify it quickly using config-set or config-edit.

If you're already running most things via Drush, it won’t take long to adapt these new commands. It's very likely you'll even prefer them over the alternatives. The key is to get out there and start using them until they become second nature.

If you're still new to Drush, take baby steps. You might also consider reading the Drush articles in previous issues: Part One: The Can Opener and Part Two: The Saw and the Blade

One last thing...

If you ask me, no great work is complete without an Easter Egg. The next Drush command you issue, add “--druplicon” to the end.

Image: ©