I am working on a project where we use Drush commands a lot …. seriously like A LOT.. That said, while working on the project, I found few drush commands which come quite handy in our day to day operations. We use these commands mostly with deploy hooks and also while working locally.
Everyone uses Drush commands which are kinda in daily use while working on Drupal and we just can’t imagine our life without it in terms of increasing our productivity.
We want to be more and more productive right? Yes, we do. :)
A little bit about Drush
Drush is an awesome command line shell and Unix scripting interface for Drupal. It is a very useful tool and its core ships with lots of useful commands for interacting with code like modules/themes/profiles. It does a lot of things like running update.php, executes sql queries and DB migrations, run cron or clear cache etc.
Even Though there are a bunch of commands available and can be found here, which are continuously making our lives easier, we still feel the urge to have custom Drush commands that suits our project needs and can improve overall team efficiency.
Creating custom Drush commands is very easy, Drupal provides hooks to integrate with our custom modules. Infact, there is not much difference between creating custom Drush commands with Drupal 7/ Drupal 8.
Coming back to this post
Here I am gonna mention few custom Drush commands which can be really handy while working on a project and can save plenty of your time.
Following are the things we should have automated using Drush commands:
- Database checks: This includes checking the number of threats with respect to the database, as if any unwanted module is enabled, PHP filter, ensuring errors and warnings are off etc.
- Clearing specific cache: This is useful when we would like to target a specific cid and bin using cache_clear_all() via the Drush command.
- Rebuilding theme (This is not similar to clearing theme-registry): There is no option as of now to rebuild theme when we add CSS or JS files in a themes .info file.
The first step, we need is to call the hook provided by Drupal. This is required to define the description of the hook, defining an alias with an example.
Now what is required is to create the callback function with whatever things we want to check to ensure that there isn’t any potential threat to the system. Following are the things we must have:
- Checking if PHP Errors and warnings are off.
- Checking if all the unnecessary modules (For example: Devel, Since XSS vulnerabilities are the #1 vulnerability found in Drupal 7 contrib modules, it’s always recommended to disable the ‘devel’ module on production sites and (if possible) entirely remove it.) are disabled. (We will just add the following to the above callback, also we can easily add more modules the list).
- Checking if USER ID 1 is blocked or not. The Drupal account created during installation (i.e. with user ID or UID=1) behaves differently from others. Primarily, it can bypass all access callbacks: it has permission to do everything by default. Failing to secure this account could result in potential security risks. Treat the UID=1 account as you would with root on Linux systems.
- Checking if PHP filter is enabled or not. There are a lot of downsides when we are using PHP filter and apparently, there is a pretty awesome explanation for that on this post.
For all these checks, we just need to go to the terminal and run the following command:
Clearing specific cache
We need to follow the same step i.e. we need is to call the hook provided by Drupal for defining the command definition, description etc.
Now, we need to define the callback which will do the fun part:
To clear cache id or array of cids from specific cache table, we just need to go to the terminal and run the following command:
Rebuilding theme data
Since we know how to define an custom drush command, we can just skip to the callback directly. ;)
Now we can rebuild theme data easily with a custom drush command.
Similarly, we can add custom Drush commands as per our use and project’s need and obviously we can improvise the scripts, do additional work. This post covers important Drush commands which are essential in our day to day projects plus it also explains how we can create custom Drush commands, YAY BONUS!! :D