One of the problems that Drush solves for developers is the automation and optimization of routine tasks. Drush commands attempt to speed up workflows and tasks that developers and site maintainers would otherwise have to do manually through the UI, or run one-by-one via the command line. One of those tasks is the process of deploying changes to a Drupal application from one environment to another.
The typical Drupal deployment process consists of repeatable steps such as importing configuration changes, applying database updates, and clearing the cache. Drush comes with the handy drush deploy
command that allows you to automate the execution of all of these tasks post code deployment.
In this tutorial we'll:
- Learn about the
drush deploy
command - Discuss when you would use the
deploy
command
By the end of this tutorial you'll know how to use the drush deploy
command in conjunction with other useful deployment-related commands to help automate the task of deploying changes to a Drupal site's configuration and code.
Some Drush commands return a lot of information -- lists of modules, generators, and status reports, for example. It might be hard to find a property you need in the small command window output. Luckily, the output of Drush commands can be piped to other commands, used as a source for imports, settings for CI, and other DevOps tasks.
In order to accommodate all these different use cases, Drush comes with a formatting system that allows you to format and filter output to meet your needs. This system allows to you specify what fields you want returned when the output contains more than one field. It also allows Drush command output to be formatted as JSON, XML, raw PHP, a table, and more.
In this tutorial we'll:
- Learn how to specify the output format -- and what formats are available
- Limit the fields that are returned in a report
- Learn to filter the output to only the data we're interested in
By the end of this tutorial you'll know how to format a Drush command's output to fit your needs.
If you want to change the way an existing Drush command works you use hooks. Hooks are useful for altering command parameters, options, annotation data, and adding custom logic during particular stages of the command execution process. Drush hooks are conceptually similar to Drupal hooks.
Hooks are methods on a Drush command class with an annotation indicating what hook is being implemented, and thus when the code should be invoked. The code in the methods is executed during specific stages of the command cycle. Developers can use core hooks -- predefined methods that come with Drush core -- or declare a custom hook that other commands can use.
In this tutorial we'll:
- Explore the different core Drush hooks
- Learn which hooks are called at what stage of the command cycle
- View example hook annotations and method implementations
By the end of this tutorial you'll know what types of core hooks are available to you and when they are called.
Adding logging and error handling are an important part of authoring Drush commands. Logs allow developers to get timely feedback from a command and inform users about potential alterations and flags, events to monitor, and the progress of long-running commands. Correct error handling allows for clean exits, meaningful error descriptions, and provides a path forward for developers to fix the errors and accomplish their goals.
In this tutorial we'll:
- Explore the different types of logging messages Drush commands can output
- Learn how to handle errors from within a Drush command
By the end of this tutorial you'll know what types of log messages you can use; how to log success messages, errors, or debug statements from a custom Drush command; and how to handle errors and exceptions.
When the logic of a command depends on user input, it's useful to set up an interactive questionnaire inside the command code. This allows you to provide the user with more context about the input they're providing, and ensure that you collect all the necessary values. This is especially useful when the command uses a pre-defined list of options and the values require memorization. An example of this is the drush cache-clear
command that comes with Drush core. It requires an argument indicating which cache to clear, which you can specify at the command line; however, if you invoke the command with no arguments it will present you with a list of cache bins to choose from and a UI for selecting one.
Drush 9+ can access the Input/Output (I/O) object via the $this->io()
method. This object -- an instance of \Drush\Style\DrushStyle
-- holds information about user-provided input, and utilities for manipulating that input. To ask a user a question, use an io()
object in the command callback method. It can take over the execution flow of the command as needed to stop and gather additional input. The I/O system has various methods for asking confirmation or choice questions such as confirm()
and choice()
.
In addition to prompting for input, the I/O object can be used to provide other styling to the command, like progress bars.
In this tutorial we'll:
- Learn how to prompt the user for additional input
- Process the user's answer as part of the command execution flow
By the end of this tutorial you should understand how to prompt a user for additional input for a custom Drush command.
While Drush empowers all Drupal users with its commands, it's even more powerful when used in combination with scripting solutions such as Composer and Bash. Scripts can be used to power post-deployment tasks like importing new configuration or clearing the cache, as part of CI processes to sync a database from one environment to another, to run background processes on the server such as imports and migrations, search indexing, running cron, and much more. If you want to write Bash (or any other scripts) that interact with a Drupal site, then Drush is the tool for you.
In this tutorial we'll:
- Learn how to use Drush commands within Composer and Bash scripts
- Learn how to chain multiple Drush commands together in a script
By the end of this tutorial you'll know how to use Drush as part of a script that automates common or tedious tasks.
When you manage many Drupal websites, you may perform repetitive tasks that are common across all of your sites. In our experience, this usually relates to having a personal preference for how certain tasks are accomplished. For example: maybe you like to make backups of the database and files in a specific way before testing upgrades, or you have a set of scripts for running scans of core web vitals. Although these tasks can be bundled into a custom module, it could be useful to create a site-wide Drush command instead. Site-wide commands can be installed with Composer, managed in a separate Git repository, and act as a project dependency. This way they are easy to maintain through a separate upstream. Changes to this code will be reflected on all the sites where it's used.
In this tutorial we'll:
- Declare a custom site-wide Drush command
- Demonstrate how to use Composer to manage a package that contains a Drush command
By the end of this tutorial you'll be able to create a site-wide Drush command and manage the code with Git and Composer.
Drupal User Guide
GuideWhat Is Drupal?
FreeTo learn more about how to get started with Drupal, also see our Introduction to Drupal guide.
In this lesson we take a look at Drupal, the open source Content Management System. We learn about the major components of a Drupal site and the library of constantly evolving tools available for working with Drupal. We find out how to see who uses it, by looking at DrupalShowcase.com and Drupal case studies on Drupal.org, among others. We look at Drupal's major features, such as Modules.
With this overview we have some context as we move forward into the series.
Additional resources
In this lesson we're going to break down the "Drupal stack" to understand what pieces are working here and how they relate to each other. We'll take a tour of the major components of Drupal itself, like nodes and users, and then have a brief discussion about ways of organizing content.
Additional resources
In this lesson we're going to start building our site for Mom and Pop, Inc. We're starting off by working on our content, to get things filled out a little. First, we'll create an About Us page with the basic info for the store, like address and opening hours. Then we'll create our first article, which welcomes people to the new site, and see how the two core content types, Basic page and Article, are different from each other. Then we'll tweak our main navigation so our tabs display correctly, and finish things off by looking at block management. We'll remove the "Powered by Drupal" block and add a new block to the site for our weekly specials.
Additional resources
This lesson offers a tour of some of the most basic tools for content management in Drupal. We're going to walk through and explain the following pieces:
- Content
- Comments
- Navigation
- Blocks
Additional resources
For this lesson we're going to get hands-on and play around in our administration area a little. We're going to review the site information for our new site, and then change things by setting the site name to match our case study.
Additional resources
In this lesson we're going to start off with the Drupal administrative interface. We'll take a tour of the major elements and get you oriented to what is where.
Additional resources
Modules allow you to turn on and off functionality within your Drupal website. There are two types of modules: core modules, which come with Drupal itself, and contributed modules, which are provided for free by the Drupal community and available for download from Drupal.org. In this lesson we're going to look at the module administration page, and walk through finding and installing modules. We'll wrap things up by discussing how you remove a module from your site, and the difference between disabling and uninstalling.
Additional resources
The easiest way to wrap your head around how modules work is to try installing and configuring a couple of them. This section will cover how to install, enable, and configure a contributed module called Using Drupal, 2nd edition
One of the most powerful features of Drupal is its rich, fine-grained access control system, based around the concept of users, roles, and permissions. In this lesson we'll explain what these pieces are, and how they work together. We'll also discuss the user profile and various user account settings we can manipulate.
Additional resources
When you open the floodgates for your users to become active participants in content creation, one of the inevitable things that comes up is the issue of content moderation—that is, ensuring that abusive, vulgar content and unsolicited advertising or spam is kept off the site and stays off. In this lesson, we'll look at using an automated spam detection, with Mollom, as well as manual spam prevention.
Additional resources
Drupalize.Me Guide: Using Drupal Book by O'Reilly Media
Note: Mollom is EOL (End Of Life). See this blog post by Drupal community member Jeff Geerling for alternative solutions.
Drupal’s built-in Search module offers powerful, flexible searching features and intelligent ranking of results. Behind the scenes, it’s silently building an index of all the words used in the site’s content. In this lesson we'll:
- Review the Search module settings
- Explain the importance of cron
- Discuss searching with Views
Additional resources
To transform the Product Finder page into a searchable index, we’ll be adding two new filters to the view: one that restricts the results by manufacturer, and another that restricts results to reviews that mention specific words. In this lesson, we'll:
- Add a filter
- Expose a filter
- Set permissions