ما هي الطريقة الصحيحة لتغيير السمة النشطة Drupal برمجيا؟
حل دروبال 6:
تريد التأكد من تغيير العالمية $custom_theme
متغير إلى حد ما في وقت تنفيذ الصفحة.
global $custom_theme;
$custom_theme = 'garland';
أعلم أنك سألت عن كيفية القيام بذلك برمجياً ، ولكن إذا كان هذا هو الحل الخاص بك ، وليس المشكلة الفعلية ، فيمكنك أيضًا استخدام وحدة ThemeKey . هذا يسمح لك بتعيين الشروط التي ، عند استيفائها ، تغير السمة. يمكنك وضع شروط استنادًا إلى المسارات والتصنيف ونوع المحتوى وإنشاء تاريخ أو تعديله والمزيد. يمكنك أيضًا إضافة وحدة خصائص سمات المفتاح الوحدة للحصول على المزيد من الخيارات.
مرة أخرى ، أعلم أن هذا ليس برمجيًا ، لكنني لست متأكدًا مما إذا كان السؤال الحقيقي وراء سؤالك هو كيفية تغيير الموضوعات بناءً على الظروف.
أفضل طريقة للقيام بذلك هي إنشاء ربط تحديث في وحدة نمطية:
function yourmodule_update_N() {
variable_set('theme_default','yourtheme');
}
في 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 ().
لاحظ أن إرجاع سمات مختلفة لنفس المسار قد لا يعمل مع التخزين المؤقت للصفحة. من المحتمل أن تكون هذه مشكلة إذا كان يمكن للمستخدم المجهول على مسار معين أن يعيد مواضيع مختلفة في ظل ظروف مختلفة.
نظرًا لأنه يمكن استخدام نسق واحد فقط في كل مرة ، فإن الوحدة النمطية الأخيرة (أي الأعلى وزنًا) التي تُرجع اسم سمة صالحًا من هذا الخطاف سوف تسود.
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);
في ما يلي وظيفة صغيرة لإعادة تعيين المظاهر بأمان إلى الافتراضي Drupal ثيمات مثل Bartik أو Garland (تم اختبارها في Drupal 6 و 7):
/**
* 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();
}
$config['system.theme']['default'] = 'my_custom_theme';
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'machine_name')
->save();