7

I saw the interesting answer to the question about "How can I get a list of content types with drush?", which starts like so:

You can create a drush command named content-type-list.

But I wonder what it should look like if the command should be named something else, eg mijn-contenttypes-lijst (instead of content-type-list), and use drush drush mcl (instead of drush ctl)?

Some reasons why I may want to use another name:

  • Because it conflicts with something else in Drupal for which I already used content-type-list and/or ctl.
  • The command is hard to remember (eg because it's not in my native language), so I want to adapt it to another name.

What exactly would I have to change in the code included in that answer, which currently looks like so:

<?php
/**
 * @file
 * Drush commands related to Content Types.
 */

/**
* Implements hook_drush_command().
*/
function drush_content_types_drush_command() {
  $items['content-type-list'] = array(
    'description' => dt("Show a list of available content types."),
    'aliases' => array('ctl'),
  );
  return $items;
}

/**
 * Callback for the content-type-list command.
 */
function drush_drush_content_types_content_type_list() {
  $content_types = array_keys(node_type_get_types());
  sort($content_types);

  drush_print(dt("Machine name"));
  drush_print(implode("\r\n", $content_types));
}

PS: I understand I can create aliases for it also (as shown in the linked answer), but that's not what my question is about. E.g. if I want a command that does something different.

0

4 Answers 4

6

Using the code in my answer to "How can I get a list of content types with drush?", you can do something like this:

The name of your file is MY_MODULE_NAME.drush.inc

<?php
/**
 * @file
 * Drush commands related to Content Types.
 */

/**
* Implements hook_drush_command().
*/
function MY_MODULE_NAME_drush_command() {
  $items['MY-DRUSH-CMD'] = array(
    'description' => dt("Show a list of available content types."),
    'aliases' => array('MD-CMD', 'another-alias', 'and-other'),
  );
  return $items;
}

/**
 * Callback for the MY-DRUSH-CMD command.
 * See here that you must change the - by a _
 * (MY-DRUSH-CMD by MY_DRUSH_CMD) in the callback function
 */
function drush_MY_MODULE_NAME_MY_DRUSH_CMD() {
  $VAR = array_keys(node_type_get_types());
  sort($VAR);

  drush_print(dt("Machine name"));
  drush_print(implode("\r\n", $VAR));
}

In the aliases array you can add more aliases.

Install the module, run drush cc drush to clear the drush cache and use the command like this:

You can use:

drush MY-DRUSH-CMD
drush MD-CMD
drush another-alias
drush and-other

References:

2
  • Hm, that looks like an answer from a teacher (with the upper cases, etc) that is tough to not understand. However I still have 1 minor curiosity (to make it fully compliant with my "contenttypes" equivalent of your original "content_types": would you agree that I can safely change the 3 occurences of "content_types" in your answer to "contenttypes"? Extra curiosity: is $conten_type (as variabe naem) intentional (to avoid some conflict), or just a typo? Jul 6, 2016 at 16:59
  • @Pierre.Vriens I made some changes, to explain better the answer, take a look and tell me if you have others questions Jul 6, 2016 at 17:06
7

You would have to change a couple of things:

/**
 * Implements hook_drush_command().
 */
function drush_content_types_drush_command() {
  $items['mijn-contenttypes-lijst'] = array(
    'description' => dt("Show a list of available content types."),
    'aliases' => array('mcl'),
  );
  return $items;
}

The array key under $items and the "aliases" value under that need changed. The array key is used to identify the callback to invoke when using the drush command or alias. The callback, for whatever reason, doesn't start with the command file prefix but rather starts with "drush" and then the command file prefix and then the array key listed above replacing the dashes with underscores. So the second (callback) function would look like...

/**
 * Callback for the content-type-list command.
 */
function drush_drush_content_types_mijn_contenttypes_lijst() {
  $content_types = array_keys(node_type_get_types());
  sort($content_types);

  drush_print(dt("Machine name"));
  drush_print(implode("\r\n", $content_types));
}

Making those changes should give you what you want. More information about creating your own drush commands can be found here: Command Authoring

2
  • Nice, merci! I realize it's only in a comment, but to be consistent I'm about sure that "Callback for the content-type-list command." should be "Callback for the mijn-contenttypes-lijst command.". No? Also, what about the "content_types" part in the first function name? Jul 6, 2016 at 15:58
  • Yes, you should make those changes you mentioned. I simply missed the comments because those parts can be duplicated without causing an error. You could even add more context to what the function is doing in the comments. Jul 7, 2016 at 17:10
4

See also the drushify command on drupal.org.

drushify is a simple code generator that creates a skeleton Drush commandfile template for the module you specify.

Installation

drush dl drushify

Usage

drush @site drushify modulename
3
  • Thank you Greg, I didn't know about this one. However, is there any docu about this module (README.txt doesn't help, and there is no "Read documentation" on the project page). Do you know of any link where it shows how such generated code looks like? Jul 6, 2016 at 16:23
  • The template file used by Drushify is here: cgit.drupalcode.org/drushify/tree/templates/commandfile.tmpl. Also, just download it per the README and run it to see the output. Jul 6, 2016 at 16:32
  • I already looked at that template, but if you're teaching somebody about this, that's not an ideal example to use. That's why I asked about "generated code". To install the modue is pretty forward (standard) of course, but "per the README" (at cgit.drupalcode.org/drushify/tree/README.txt I asume) is not obvious (1 line of content). I'll do some more homework on this later on. Jul 6, 2016 at 16:42
3

... Ill preface this by saying that your question really does make it sound like you should be creating an alais, but since you are explicitly asking how to create your own brand new command with a new name, here's what you can do:

You need to change the array key in the hook_drush_command function as well as the name of the callback function. You can create a file called drush_foo and define any commands you want. Notice that the array key from the hook_drush_command function is the same as the callback function name (except hyphens are replaced with underscores). Try something like this:

<?php
/**
 * @file
 * Drush commands related to [insert description here].
 */

/**
* Implements hook_drush_command().
*/
function drush_foo_drush_command() {
  $items['mijn-contenttypes-lijst'] = array(
    'description' => dt("Do something awesome"),
    'aliases' => array('mcl'),
  );
  return $items;
}

/**
 * Callback for the mijn-contenttypes-lijst command.
 * Notice that this function name ends with mijn-contenttypes-lijst
 * except hyphens are replaced with underscores.
 */
function drush_drush_foo_mijn_contenttypes_lijst() {
  // Do something amazing (or call some other existing drush callback function)
}

Also, dont forget that whenever you add (or modify) a drush command you should ALWAYS run drush cc drush so that drush can clear its own cache and see that there is a new command

2
  • If I would create an alias of "content-type-list", and assuming I already had a "content-type-list" (which does something completely different), then I think creating an alias is going to do that very same "completely different" (instead of whatever I would want my "mijn-contenttypes-lijst" cmd to do). Or am I still missing something? Also, if my module's name would be "foo", which implements hook_drush_command, why is the name of the first function then not simply foo_drush_command? Does drush require that "drush_" at the start of the first function name? Jul 6, 2016 at 16:17
  • Your correct that you should not use an alias if you want a command that does something different ... but your question was a bit ambiguous. Looks like @adrian and I gave you essentially the same answer though, so you should be good to go
    – bleen
    Jul 6, 2016 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.