アクティブなDrupalテーマをプログラムで変更する正しい方法は?
質問
アクティブなDrupalテーマをプログラムで変更する正しい方法は何ですか?
解決
Drupal 6ソリューション:
グローバルを変更することを確認します $custom_theme
ページ実行のかなり早い段階で変数。
global $custom_theme;
$custom_theme = 'garland';
他のヒント
私はあなたがそれをプログラム的に行う方法を尋ねたことを知っていますが、それが実際の問題ではなくあなたの解決策である場合、あなたは ThemeKeyモジュール. 。これにより、テーマを変更すると、条件を設定できます。パス、分類、コンテンツタイプ、作成または編集日などに基づいて条件を作成できます。追加することもできます ThemayKeyプロパティモジュール さらに多くのオプションを取得するモジュール。
繰り返しますが、これはプログラムではないことを知っていますが、あなたの質問の背後にある本当の質問が条件に基づいてテーマを変更する方法であるかどうかはわかりません。
これを行う最良の方法は、モジュールで更新フックを作成することです。
function yourmodule_update_N() {
variable_set('theme_default','yourtheme');
}
Drushを介してアクティブなテーマを変更します
drush vset theme_default garland
drush vset admin_theme garland
drush cc all
モジュールを介してアクティブなテーマを変更します
デフォルトのテーマと管理テーマを変更する基本:
// Changes the theme to Garland
variable_set('theme_default', $theme_default);
// Changes the administration theme to Garland
variable_set('admin_theme', $admin_theme);
これは、BartikやGarland(Drupal 6および7でテストされている)などのデフォルトのDrupalテーマに安全にテーマを後退させる小さな機能です。
/**
* Set the active Drupal themes (the default and the administration theme) to default ones.
* Tested in Drupal 6, 7 (but possibly working in version 8 too according to the documentations [some similarities between 7 and 8]).
*/
function TESTMODULE_set_active_theme_to_default($affect_admin_theme = TRUE) {
// Provides a list of currently available themes.
$list_themes = list_themes(TRUE);
// 6, 7, 8, etc.
$major_version = (int)VERSION;
$theme_default = isset($list_themes['bartik']) ? 'bartik' : 'garland';
$admin_theme = isset($list_themes['seven']) ? 'seven' : 'garland';
// Changes the theme to Garland
variable_set('theme_default', $theme_default);
// Changes the administration theme to Garland if argument is TRUE
if($affect_admin_theme){
variable_set('admin_theme', $admin_theme);
}
// if Switchtheme module (https://drupal.org/project/switchtheme) is enabled, use it
if (module_exists('switchtheme')) {
if (empty($_GET['theme']) || $_GET['theme'] !== $theme_default) {
$query = array(
'theme' => $theme_default
);
// in D6, drupal_goto's second argument is the query string,
// in >=D7, a more general $options array is used
if($major_version < 7){
$options = $query;
}
else{
$options = array('query' => $query);
}
drupal_goto($_GET['q'], $options);
}
}
drupal_set_message(t('Default theme has been changed to %theme_default, administration theme has been changed to %admin_theme.', array(
'%theme_default' => $theme_default,
'%admin_theme' => $admin_theme
)));
}
あなたはそれをで呼ぶことができます hook_init() 実装(不要になった後にコメントしてください):
/**
* Implements hook_init()
*/
function TESTMODULE_init() {
// ATTENTION! Comment out the following line if it's not needed anymore!
TESTMODULE_set_active_theme_to_default();
}
Drupal 7で使用します hook_custom_theme()
:
/**
* Implements hook_custom_theme()
* Switch theme for a mobile browser
* @return string The theme to use
*/
function mymodule_custom_theme() {
//dpm($_SERVER['HTTP_USER_AGENT']);
$theme = 'bartik'; // core theme, used as fallback
$themes_available = list_themes(); // get available themes
if (preg_match("/Mobile|Android|BlackBerry|iPhone|Windows Phone/", $_SERVER['HTTP_USER_AGENT'])) {
if (array_key_exists('custommobiletheme', $themes_available)) $theme = 'custommobiletheme';
else { drupal_set_message("Unable to switch to mobile theme, because it is not installed.", 'warning'); }
}
else if (array_key_exists('nonmobiletheme', $themes_available)) $theme = 'nonmobiletheme';
// else, fall back to bartik
return $theme;
}
から適応 u003Cemoticode />
現在のページに使用するテーマの機械読み取り可能な名前を返します。
この関数へのコメントは読む価値があるかもしれません:
このフックを使用して、現在のページリクエストのテーマを動的に設定できます。動的な条件に基づいてテーマをオーバーライドする必要があるモジュールで使用する必要があります(たとえば、現在のユーザーの役割に基づいてテーマを設定できるモジュール)。 このフックの返品値は、fook_menu()のテーマコールバック関数を介してページごとまたはセクションごとの有効なテーマが設定されているページを除くすべてのページで使用されます。それらのページのテーマは、hook_menu_alter()を使用してのみオーバーライドできます。
同じパスの異なるテーマを返すと、ページキャッシングでは動作しない場合があることに注意してください。これは、特定のパス上の匿名のユーザーが異なる条件下で異なるテーマを返す可能性がある場合、問題になる可能性が最も高くなります。
一度に1つのテーマのみを使用できるため、このフックから有効なテーマ名を返す最後の(つまり、最高の加重)モジュールが勝ちます。
Drupal 8:
settings.php
$config['system.theme']['default'] = 'my_custom_theme';
設定プログラムで更新:
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'machine_name')
->save();