Create Custom Drush Commands in Drupal 8 / Drupal 9

Create Custom Drush Commands in Drupal 8 / Drupal 9

In this Blog Post, I'll show you how to create custom Drush commands in Drupal 8. 

In This Article, we are going to show how to delete an invalid user email id if any add invalid email id using DB or any import data function. you can change the logic part according to your requirement.

add unset_invalid_usermail/unset_invalid_usermail.info.yml

name: 'Unset Invalid Usermail'
type: module
description: 'Drush command to empty mail field if they have wrong value'
core: 8.x
package: 'Custom'

add unset_invalid_usermail/drush.services.yml

This file declares a service pointing to UnsetUserDrushCommands.php class and tag the service with the Drush.command tag.

services:
  unset_invalid_usermail.commands:
    class: \Drupal\unset_invalid_usermail\Commands\UnsetUserDrushCommands
    tags:
      - { name: drush.command }

add unset_invalid_usermail/src/Commands/UnsetUserDrushCommands.php

this file includes the entire definition for our custom command.

<?php

namespace Drupal\unset_invalid_usermail\Commands;

use Drush\Commands\DrushCommands;

/**
 * A Drush commandfile.
 *
 * 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 UnsetUserDrushCommands extends DrushCommands {
  /**
   * Drush command that displays the given text.
   *
   * @param string $text
   *   Argument with message to be displayed.
   * @command unset_invalid_usermail
   * @aliases uiu
   * @option unset_invalid_usermail
   *   Set empty invalid mail address.
   * @usage unset_invalid_usermail:unset_mail
   */

  public function unset_mail($text = "User Invalid mail value is Empty") {

    $database = \Drupal::database();
    $ids = $database->select('users_field_data', 't')
          ->fields('t', ['uid'])
          ->fields('t', ['mail'])
          ->condition('mail', '@', 'NOT REGEXP')
          ->execute()
          ->fetchAll();

    foreach ($ids as $key => $val) {
      $query = $database->update('users_field_data');
      $query->fields([
        'mail' => '',
      ]);
      $query->condition('uid', $val->uid);
      $query->execute();
      $final_text = $text . ' For This User ID - ' . $val->uid;
      $this->output()->writeln($final_text);
    }
  }

}
?>

Some of the annotations available for use include:

@command – command definition, which needs to follow the module: command structure;

@aliases – aliases for your commands, separated with spaces;

@param – which defines the input parameters for your command;

@option – which defines the options available for your commands, which should be put in an associative array, where the name of the option is the key;

@usage – example showing how the command should be used.
 

How to use this custom command:

How to use this Drush custom command:

Step Follow module installation: 

Step 1:- Run drush cr clear cache 

Step 2:- Enable and Install module using drush en unset_invalid_usermail

Step 3:- And Run drush uiu as you can seen in above screenshot  here we are user drush uiu because are alise command 
name as uiu short form.


Hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.

Follow us on other social media platform you can check link footer.