アクティブなDrupalテーマをプログラムで変更する正しい方法は?


回答:


15

Drupal 6ソリューション:

$custom_themeページ実行のかなり早い段階でグローバル変数を変更するようにします。

global $custom_theme;
$custom_theme = 'garland';

(カスタムモジュールを使用している場合)ページ実行の非常に早い段階で試す2つのフックは、hook_boot()とhook_init()です。
デビッドラニエ

どこで$custom_theme定義されていますか?テーマを変更するのに十分ですか?
モハマドアリアクバリ


1
成功を再現できませんでした。:<
スターロック

私のために働いた。hook_boot()
マーク

15

プログラムでそれを行う方法を尋ねたのは知っていますが、それが実際の問題ではなく解決策である場合は、ThemeKeyモジュールを使用することもできます。これにより、条件を設定して、満たされたときにテーマを変更できます。パス、分類法、コンテンツタイプ、作成日または編集日などに基づいて条件を作成できます。Themekey Propertiesモジュールモジュールを追加して、さらに多くのオプションを取得することもできます。

繰り返しますが、これはプログラムではないことは知っていますが、あなたの質問の背後にある本当の質問が条件に基づいてテーマを変更する方法であるかどうかはわかりません。


4
うん、UIでこれを管理したいなら、ThemeKeyをお勧めします。
デイ

または少なくともチェックアウトdrupalcode.org/project/themekey.git/blob/refs/heads/7.x-2.x:/ ... ThemeKeyが魔法をかける場所
Capi Etheriel

@Chaulkyは正しいです。しばらくの間、ThemeKeyを使用しています。これは、ユーザー名、ロール、ページなど、何でも好きなテーマのカスタマイズを管理する最も簡単な方法です。私はそれをお勧めします。
Benj

14

これを行う最良の方法は、モジュールに更新フックを作成することです。

function yourmodule_update_N() {
  variable_set('theme_default','yourtheme');
}

これは、正しい答えをする必要があります...
ニック・バレット

これは、テーマをグローバルに変更する場合にのみ正しいでしょう。私は、OPが特定のページまたは特定のコンテキストでテーマを変更することを望んでいたという質問から推測していました。
エヴァンドノヴァン

7

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();
}

これは、インストールプロファイルでテーマを有効にするときに使用する方法でもありますvariable_set('theme_default','yourtheme');
-Duncanmoo

7

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;
}

<emoticode />からの適応

現在のページに使用するテーマの機械可読な名前を返します。

この関数のコメントは読む価値があります。

このフックを使用して、現在のページ要求のテーマを動的に設定できます。動的な条件に基づいてテーマをオーバーライドする必要があるモジュール(たとえば、現在のユーザーの役割に基づいてテーマを設定できるモジュール)で使用する必要があります。このフックの戻り値は、hook_menu()のテーマコールバック関数によって設定された有効なページごとまたはセクションごとのテーマを持つページを除くすべてのページで使用されます。それらのページのテーマは、hook_menu_alter()を使用してのみ上書きできます。

同じパスに対して異なるテーマを返すことは、ページキャッシングでは機能しない可能性があることに注意してください。これは、特定のパスの匿名ユーザーが異なる条件で異なるテーマを返す場合に問題になる可能性が最も高くなります。

一度に使用できるテーマは1つだけなので、このフックから有効なテーマ名を返す最後の(つまり、最も重みが高い)モジュールが優先されます。


3

Drupal 8の場合:

settings.phpで

$config['system.theme']['default'] = 'my_custom_theme';

プログラムで設定を更新します。

\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'machine_name')
->save();
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.