Fermer

juillet 22, 2022

Comment créer une commande Drush personnalisée dans Drupal



Dans ce tutoriel, nous allons apprendre à créer une commande Drush personnalisée dans Drupal. Pour cela, nous devons créer un module personnalisé. La structure du fichier sera comme ci-dessous

drush_command_example
   Commands
      CustomCommands.php
   drush_command_example.info
  drush.services.yml



1. fichier drush_command_example.info

name: Drush Command Example
description: This is our custom drush command.
core: 8.x
core_version_requirement: ^8 || ^9
type: module
package: ttn

2. Fichier drush.services.yml.

services:
  drush_command_example.commands:
    class: \Drupal\drush_command_example\Commands\CustomCommands
    tags:
      - { name: drush.command }

3. Fichier CustomCommands.php.

<?php
namespace Drupal\drush_command_example\Commands;
use Drush\Commands\DrushCommands;



class CustomCommands extends DrushCommands {

  
  public function printMe($text = 'Hello world!', $options = ['uppercase' => FALSE]) {
    if ($options['uppercase']) {
      $text = strtoupper($text);
    }
    $this->output()->writeln($text);
  }
} Enable module or if you created drush command in existing module than please clear cache. you can see custom drush command in list using
drush list
We can use our newly created drush command as below.

drush ttndrush --help 
drush ttndrush "hello from to the new" 
drush ttndrush "hello from to the new" --uppercase 
drush ttndrush-print-me "hello from to the new" 
drush ttndrush-print-me "hello from to the new" --uppercase

TROUVÉ CELA UTILE ? PARTAGEZ-LE




Source link