Greg Anderson Reading estimate: 2 minutes
Modern Command Line Tools for Drupal Modules with Drush and Drupal Console
Image
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.
Topics
Discover More
America’s Underground Lab: a Research Website Creation Story
Yulia Popova
Reading estimate: 3 minutes
Pantheon and Tag1 to Provide Free Long-Term Support for Drupal 7 Websites
Chris Yates
Reading estimate: 2 minutes
Election Year Dynamics: What They Mean for Government Web Development
Yulia Popova
Reading estimate: 4 minutes