Drupal - How can I get a list of content types with drush?
List of names:
drush sqlq "SELECT name FROM node_type;"
List of machine names:
drush sqlq "SELECT type FROM node_type;"
This works in D6 and D7.
The first line of the command's output will be name
or type
, respectively.
Pipe |tail -n +2
if you want to drop that first line.
Try the following commands.
Drupal 7 & 8
drush ev "print_r(array_keys(node_type_get_types()));"
Drupal 5 & 6
drush ev "print_r(array_keys(node_get_types()));"
You can create a drush command named content-type-list
. Create a module named drush_content_types
, inside the drush_content_types.drush.inc
file put this code:
<?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));
}
Install the module, run drush cc drush
to clear the drush cache and use the command like this:
drush ctl
or
drush content-type-list
If you want add another alias to the command add elements to the aliases array like this:
'aliases' => array('ctl', 'all-content-types', 'act'),
And you can use this commands:
drush act
drush all-content-types
drush ctl
drush content-type-list
Always the output will be:
Machine name:
content 1
content 2
content...
content n