'Show current status of theme configuration in the site.',
);
$items['theme list'] = array(
'description' => 'List all available themes for the site.',
);
$items['theme list enabled'] = array(
'description' => 'List all enabled themes for the site.',
);
$items['theme info'] = array(
'description' => 'List all info about a theme.',
'arguments' => array(
'theme_name' => 'The code name of the theme',
),
);
$items['theme set default'] = array(
'description' => 'Set the default theme.',
'arguments' => array(
'theme_name' => 'The code name of the theme',
),
);
$items['theme set admin'] = array(
'description' => 'Set the administration theme.',
'arguments' => array(
'theme_name' => 'The code name of the theme',
),
);
$items['theme enable'] = array(
'description' => 'Enable a theme.',
'arguments' => array(
'theme_name' => 'The code name of the theme',
),
);
$items['theme disable'] = array(
'description' => 'Disable a theme.',
'arguments' => array(
'theme_name' => 'The code name of the theme',
),
);
return $items;
}
/**
* Implementation of hook_drush_help().
*/
function theme_drush_help($section) {
switch ($section) {
case 'drush:theme status':
return dt('Show current status of theme configuration in the site. It shows the default theme, the administration theme and other enabled themes.');
case 'drush:theme list':
return dt('List all available themes for the site. It shows each theme name, title and current status (enabled/disabled/default/administration). It works with --pipe');
case 'drush:theme list enabled':
return dt('List all enabled themes for the site. It shows each theme name, title and if theme is default or administration one. It works with --pipe');
case 'drush:theme info':
return dt('List all info about a theme. Including: php version, regions, features and files (css and js).');
case 'drush:theme set default':
return dt('Set the default theme. It will enable the theme prior to setting as default in not yet enabled.');
case 'drush:theme set admin':
return dt('Set the administration theme.');
case 'drush:theme enable':
return dt('Enable a theme.');
case 'drush:theme disable':
return dt('Disable a theme. It will fail if the theme is set as default theme.');
}
}
/**
* Implementation of command drush theme status
.
*
* This function prints the current status of theme configuration in the site.
*/
function drush_theme_theme_status() {
$theme_default = _drush_theme_get_default_theme();
$admin_theme = _drush_theme_get_admin_theme();
$enabled = _drush_theme_get_enabled_themes();
$key = array_search($theme_default, $enabled);
unset($enabled[$key]);
drush_print(dt("Default theme: @theme_default", array('@theme_default' => $theme_default)));
drush_print(dt("Administration theme: @admin_theme", array('@admin_theme' => $admin_theme)));
if (count($enabled) > 0) {
drush_print(dt("Other enabled themes: ") . implode(' ', $enabled));
}
}
/**
* Implementation of command drush theme list
.
*
* This function prints the list of available themes and their status.
*/
function drush_theme_theme_list() {
$themes = _drush_theme_get_themes();
$theme_default = _drush_theme_get_default_theme();
$admin_theme = _drush_theme_get_admin_theme();
$enabled = _drush_theme_get_enabled_themes();
$rows[] = array(dt('Name'), dt('Title'), dt('Enabled/Disabled'), dt('Default'), dt('Administration'));
foreach ($themes as $theme) {
$default = ($theme->name == $theme_default ? dt('Yes') : dt('No'));
$admin = ($theme->name == $admin_theme ? dt('Yes') : dt('No'));
$status = (in_array($theme->name, $enabled) ? "enabled" : "disabled");
$rows[] = array($theme->name, $theme->info['name'], $status, $default, $admin);
$pipe[] = $theme->name;
}
drush_print_table($rows, TRUE);
drush_print_pipe(implode(' ', $pipe));
}
/**
* Implementation of command drush theme list enabled
.
*
* This function prints the list of enabled themes and their status
* (default/administration).
*/
function drush_theme_theme_list_enabled() {
$themes = _drush_theme_get_themes();
$theme_default = _drush_theme_get_default_theme();
$admin_theme = _drush_theme_get_admin_theme();
$enabled = _drush_theme_get_enabled_themes();
$rows[] = array(dt('Name'), dt('Title'), dt('Default'), dt('Administration'));
foreach ($themes as $theme) {
if (!in_array($theme->name, $enabled)) {
continue;
}
$default = ($theme->name == $theme_default ? dt('Yes') : dt('No'));
$admin = ($theme->name == $admin_theme ? dt('Yes') : dt('No'));
$rows[] = array($theme->name, $theme->info['name'], $default, $admin);
$pipe[] = $theme->name;
}
drush_print_table($rows, TRUE);
drush_print_pipe(implode(' ', $pipe));
}
/**
* Implementation of command drush theme info «theme_name»
*
* This function prints information about a theme.
*
* @param $theme_name The machine name of the theme
*/
function drush_theme_theme_info($theme_name) {
$theme = _drush_theme_get_theme($theme_name);
drush_print(dt("Name: ") . $theme->name);
if (drush_drupal_major_version() > 5) {
drush_print(dt("Title: ") . $theme->info['name']);
drush_print(dt("Description: ") . $theme->info['description']);
drush_print(dt("Version: ") . $theme->info['version']);
drush_print(dt("Core: ") . $theme->info['core']);
drush_print(dt("PHP: ") . $theme->info['php']);
drush_print(dt("Engine: ") . $theme->info['engine']);
drush_print(dt("Regions: ") . implode(', ', $theme->info['regions']));
drush_print(dt("Features: ") . implode(', ', $theme->info['features']));
drush_print(dt("Stylesheets: "));
foreach ($theme->info['stylesheets'] as $media => $files) {
drush_print(dt("media: ") . $media, 2);
drush_print(dt("files: ") . implode(', ', $files), 4);
}
if (count($theme->info['scripts']) > 0) {
drush_print(dt("Scripts: "));
drush_print(dt("files: ") . implode(', ', $theme->info['scripts']), 2);
}
}
else {
drush_print(dt("Filename: ") . $theme->filename);
drush_print(dt("Description: ") . $theme->description);
}
}
/**
* Implementation of command drush theme set default «theme_name»
*
* This function sets «theme_name» as the default theme for the site. It enables
* it if not already enabled.
*
* @param $theme_name The machine name of the theme
*/
function drush_theme_theme_set_default($theme_name) {
_drush_theme_valid_theme_or_die($theme_name);
db_query("UPDATE {system} SET status = 1 WHERE type = 'theme' and name = '%s'", $theme_name);
variable_set('theme_default', $theme_name);
drush_log("Setting $theme_name as default theme", "success");
}
/**
* Implementation of command drush theme set admin «theme_name»
*
* This function sets «theme_name» as the administration theme for the site.
*
* @param $theme_name The machine name of the theme
*/
function drush_theme_theme_set_admin($theme_name) {
_drush_theme_valid_theme_or_die($theme_name);
variable_set('admin_theme', $theme_name);
drush_log("Setting $theme_name as admin theme", "success");
}
/**
* Implementation of command drush theme enable «theme_name»
*
* This function enables «theme_name» in the site.
*
* @param $theme_name The machine name of the theme
*/
function drush_theme_theme_enable($theme_name) {
_drush_theme_valid_theme_or_die($theme_name);
db_query("UPDATE {system} SET status = 1 WHERE type = 'theme' and name = '%s'", $theme_name);
drush_log("Enabling $theme_name", "success");
}
/**
* Implementation of command drush theme disable «theme_name»
*
* This function disables «theme_name» if it is not the default theme for the
* site.
*
* @param $theme_name The machine name of the theme
*/
function drush_theme_theme_disable($theme_name) {
_drush_theme_valid_theme_or_die($theme_name);
$theme_default = _drush_theme_get_default_theme();
if ($theme_name == $theme_default) {
drush_die("$theme_name is the default theme. You must set another theme as default before trying to disable this one.");
}
db_query("UPDATE {system} SET status = 0 WHERE type = 'theme' and name = '%s'", $theme_name);
drush_log("Disabling $theme_name", "success");
}
/// HELPER FUNCTIONS
/**
* This helper function retrieves all themes available for the site.
*/
function _drush_theme_get_themes() {
$themes = list_themes();
return $themes;
}
/**
* Die if «theme_name» is not an available theme for the site.
*
* This helper function makes drush die if «theme_name» is not a valid name
* of an available theme in the site.
*
* @param $theme_name The machine name of the theme
*/
function _drush_theme_valid_theme_or_die($theme_name) {
$themes = _drush_theme_get_themes();
if (!array_key_exists($theme_name, $themes)) {
drush_die("Unknown theme");
}
}
/**
* This helper function returns the theme object for «theme_name».
*
* @param $theme_name The machine name of the theme
*/
function _drush_theme_get_theme($theme_name) {
_drush_theme_valid_theme_or_die($theme_name);
$themes = _drush_theme_get_themes();
return $themes[$theme_name];
}
/**
* This helper function returns the name of the default theme for the site.
*/
function _drush_theme_get_default_theme() {
$theme_default = variable_get('theme_default', 'garland');
return $theme_default;
}
/**
* This helper function returns the name of the administration theme for the site.
*/
function _drush_theme_get_admin_theme() {
$theme_default = _drush_theme_get_default_theme();
$admin_theme = variable_get('admin_theme', $theme_default);
return $admin_theme;
}
/**
* This helper function returns the enabled themes' objects.
*/
function _drush_theme_get_enabled_themes() {
$enabled = array();
$themes = _drush_theme_get_themes();
foreach ($themes as $theme) {
if ($theme->status == 1) {
$enabled[] = $theme->name;
}
}
return $enabled;
}