Drupa 8 Custom Drush Command

If you need to create a custom drush command here is how this example Sets the home page via path not via node id (this one uses another core drush command)

First you need a module and a file in it that is has .drush.inc at the end of it

Example mymodule.drush.inc

then you need to do some thing like this:

<?php

/**
 * @file
 * Contains mymodule.drush.inc.
 */

/**
 * Implements hook_drush_command().
 */
function mymodule_drush_command() {
  $items = [];
  $items['set-front-page'] = [
    'description' => 'Run this to set front page tp /home',
    'aliases' => ['cust:set-front-page'],
  ];
  return $items;
}

/**
 * Call back function for drush_[MODULE_NAME]_[COMMAND_NAME]().
 */
function drush_mymodule_set_front_page() {
  // Set front page to "/home".
  $path = \Drupal::service('path.alias_manager')->getPathByAlias('/home');
  // This is the same as drush cset but used in our custom command
  drush_config_set('system.site', 'page.front', $path);
  drush_print('Front Page set');
}

 

Then you need to run "drush cc drush" to clear drush cache ...

then run your new command with "drush cust:set-front-page". the worst part of this is the naming conventions as noware do you set the call back, drush just knows
"with drush_[MODULENAME]_[COMMAND_NAME]()"
 

as you can see above set-front-page became set_front_page.

Category: