Modern Command Line Tools for Drupal Modules with Drush and Drupal Console

Greg Anderson Reading estimate: 2 minutes

Image

Morpheus Drush Console Meme

Having a command line interface to the functionality provided by Drupal modules has been a highly valuable and widely-used feature that has been used for many years. Today, there are over 500 Drupal modules that provide Drush commands, and the number keeps growing.  On top of this, some modules have started to use Drupal Console to implement their command line tools. Drupal Console provides an object-oriented interface and a host of utility functions provided by the Symfony Console libraries. The Drupal Console APIs are useful, but the existence of two standards has led to some duplication of functionality, which increases maintenance costs. This is particularly acute for features such as site install and configuration import/export, which occasionally need to be adjusted to keep pace with changes made in Drupal core. This situation also creates some angst for module developers, who must decide which API will be best for their project.

Recently, the Drush and Drupal Console maintainers started a collaborative effort to make an even better way to use to create command line tools for Drupal modules rapidly. The new system uses annotations to describe the characteristics of your command. That is literally all that is required to make your command work with both Drush and Drupal Console. The command line tool framework just gets out of your way, and lets you call your module APIs to do what you need to do.

There is already a preliminary implementation of one of these “annotated command files” available for the Drupal 8 default_content module that provides two commands to export content. The basic structure is shown below:

class DefaultContentCommands
{
    /**
     * Exports a single entity.
     *
     * @param string $entityType The type of the entity to export.
     * @param string $entityId The id of the entity to export.
     * @option string $file Write out the exported content to a file
     *         instead of stdout
     * @aliases dcex
     */
    public function defaultcontentExport(
        $entity_type_id,
        $entity_id,
        $options = ['file' => ''])
    {
        // … implementation
    }

    /**
     * Exports an entity and all its referenced entities.
     *
     * @param string $entityType The type of the entity to export.
     * @param string $entityId The id of the entity to export.
     * @param string $folder Folder to export to, entities are grouped
     *         by entity type into directories
     * @aliases dcexr
     */
    public function defaultcontentExportReferences(
        $entity_type_id,
        $entity_id,
        $entity_type_folder)
    {
        // … implementation
    }
}

 

The entire command is available in a patch in the default_content issue queue, Modern CLI command. It works with pre-release versions of both Drush and Drupal Console, as long as you have installed the appropriate PR: Use Symfony Console application and annotation commands in Drush, or Register annotation commands in Drupal Console, respectively.

Progress is being made rapidly on this and other new features that will make the task of providing a command line interface to your module quick and easy. Be sure to catch our session at DrupalCon New Orleans, Writing Command Line Tools for Drupal 8 Modules, to learn all about it.

Discover More

Halting DDoS Attacks: Effective Strategies for Prevention

Conor Bauer
Reading estimate: 9 minutes

Staying Ahead In The Game of Distributed Denial of Service Attacks

Steve Persch
Reading estimate: 3 minutes

Learn how healthcare sites can thrive with modern development tools

Jason Yarrington
Reading estimate: 3 minutes

Try Pantheon for Free

Join thousands of developers, marketers, and agencies creating magical digital experiences with Pantheon.