Domanda

Qual è il modo corretto di cambiare il tema attiva Drupal programmazione?

È stato utile?

Soluzione

Drupal 6 Soluzione:

Si vuole fare in modo di cambiare il globale $custom_theme variabile abbastanza presto in esecuzione della pagina.

global $custom_theme;
$custom_theme = 'garland';

Altri suggerimenti

So che hai chiesto come farlo programattically, ma nel caso in cui questo è la soluzione, non il problema vero e proprio, è anche possibile utilizzare il ThemeKey modulo . Questo permette di impostare le condizioni che, se soddisfatte, cambia il tema. È possibile effettuare le condizioni basate su percorsi, tassonomia, tipo di contenuto, creare o modificare la data e molto altro ancora. È inoltre possibile aggiungere nel Themekey Proprietà modulo modulo per ottenere ancora più opzioni.

Ancora una volta, so che questo non è programmattically, ma non sono sicuro se la vera domanda dietro la domanda è come cambiare i temi in base alle condizioni.

Il modo migliore per farlo è quello di creare un gancio aggiornamento in un modulo:

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

Modifica del tema attivo tramite Drush

drush vset theme_default garland
drush vset admin_theme garland
drush cc all

Modifica del tema attivo tramite un modulo

Le basi di cambiare il tema di default tema e amministrazione:

// Changes the theme to Garland
variable_set('theme_default', $theme_default);
// Changes the administration theme to Garland
variable_set('admin_theme', $admin_theme);

Ecco una piccola funzione per impostare di nuovo i temi in modo sicuro per temi di Drupal come Bartik o Garland (testato in Drupal 6 e 7) di default:

/**
 * 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
  )));

}

Si può chiamare in un hook_init () implementazione (commento fuori dopo non è necessaria):

/**
 * Implements hook_init()
 */
function TESTMODULE_init() {  
  // ATTENTION! Comment out the following line if it's not needed anymore!
  TESTMODULE_set_active_theme_to_default();
}

In Drupal 7, uso 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;
}

Ritorna il nome leggibile dalla macchina del tema da utilizzare per la pagina corrente.

I commenti per questa funzione potrebbe essere la pena di leggere:

Questo gancio può essere utilizzato per impostare dinamicamente il tema per la corrente richiesta di pagina. Dovrebbe essere usato dai moduli che devono sostituire la tema basato su condizioni dinamiche (per esempio, un modulo che permette il tema da impostare in base al ruolo dell'utente corrente). Il ritorno valore di questo hook è utilizzato su tutte le pagine tranne quelli che hanno un valida per pagina o per-sezione set tema tramite una funzione di callback tema in hook_menu (); i temi su tali pagine possono essere solo sovrascritti con hook_menu_alter ().

Si noti che il ritorno temi diversi per lo stesso percorso potrebbe non funzionare con il caching delle pagine. Questo è più probabile che sia un problema se un anonimo utente su un determinato percorso potrebbe avere diversi temi restituiti sotto diverse condizioni.

Dal momento che un solo tema può essere utilizzato in un momento, l'ultima (vale a dire, più alto modulo ponderata) che restituisce un nome tema valido da Questo hook prevalere.

Per Drupal 8:

In settings.php

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

Aggiornamento config programmaticamente:

\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'machine_name')
->save();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a drupal.stackexchange
scroll top