Use drush to generate module & drush commands

In this post we are going to learn how to creating module and drush commands using drush.

First we will create a module using drush command. Lets run the following drush command and follow the instruction shown in below picture:


  drush generate module-standard
  

Here we have created a module with name content_processor, now let's create a drush command in same module which will process node content based on provided node type.

Run the following drush command to generate command boilerplate code, further we'll modify as per our need.


  drush generate drush-command-file
  

As we have created a drush command boilerplate code along with module the structure should look like below:

Now, let's update the ContentProcessorCommands.php class code by adding below functions:


    namespace Drupal\content_processor\Commands;

    use Drush\Commands\DrushCommands;

    /**
     * A Drush command file.
     *
     * In addition to this file, you need a drush.services.yml
     * in root of your module, and a composer.json file that provides the name
     * of the services file to use.
     *
     * See these files for an example of injecting Drupal services:
     *   - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
     *   - http://cgit.drupalcode.org/devel/tree/drush.services.yml
     */
    class ContentProcessorCommands extends DrushCommands {
      /**
       * A command to process node content based on provided type.
       *
       * @param $node_type
       *   Node types whose content has to process.
       * @param array $options
       *   An associative array of options whose values come from cli, aliases, config, etc.
       * @option batch
       *   Number of item which should be process in one go.
       * @usage content-processor:process-content page --batch=50
       *   Usage description
       *
       * @command content-processor:process-content
       * @aliases cppc
       */
      public function processContentCommand($node_type, $options = ['batch' => '10']) {
        // Add your logic to process the content
        // or may be create a service which will the work
        // and just call that service from here
        $this->logger()->success(dt('Processing node...'));
      }

      /**
       * A validate hook for content-processor:process-content command.
       *
       * @hook validate content-processor:process-content
       *
       * @param \Consolidation\AnnotatedCommand\CommandData $commandData
       *   The command data.
       *
       * @return \Consolidation\AnnotatedCommand\CommandError|null
       *   The command data or null.
       */
      public function validateProcessContentCommand(CommandData $commandData) {
        // This is the best place to validate all your command input
        // either its interactive or non-interactive, ex node_type,
        // let assume we want to check the provide node type should be valid one.
        $node_type = $commandData->input()->getArgument('node_type');
        $allowed_node_types = ['article','page'];
        $message = [];
        if (!in_array($node_type, $allowed_node_types)) {
          $message[] = 'Invalid node type';
        }
        if ($message) {
          return new CommandError(implode(' ', $message));
        }
      }

      /**
       * This hook will be trigger just after the command.
       * @hook post-command content-processor:process-content
       */
      public function processContentPostCommand($result, CommandData $commandData) {
        // Do something after content-processor:process-content
        $this->logger()->success(dt('performing some cleanup...'));
      }
    }
  

Note: Here we have used validate and post hook command.

Run the below command to validate its working as expected


  drush content-processor:process-content article
  

Comments

Popular posts from this blog

How to setup Drupal 8 Multisite on nginx webserver with different domain.

Install drush globally using composer on WSL

Drupal views create exposed filter programmatically