在Drupal的后台,每个drupal主题都拥有一个配置页面,位于admin/build/themes/settings/themeName。在该页面中有一个表单,里面包含一些标准设置,比如“Logo image settings” 和“Shortcut icon settings.”。
现在,在Drupal 6中,drupal主题作者可以通过向该表单添加额外的设置来定制这个页面。在Drupal 5中,主题作者和主题用户首先需要安装主题设置API模块(5.x-2.1或者最新版本),这样才能使用下面介绍的方法。
首先,在你的drupal主题目录下面,创建一个theme-settings.php文件,并添加一个themeName_settings() 或者themeEngineName_settings()函数。最好是用themeEngineName_settings()表单,这样他人会更容易的基于你的主题创建子主题。在函数中,应该使用表单API来创建要添加的表单小部件。
例如:为了向Garland主题添加设置,需要在drupal主题的theme-settings.php文件中添加garland_settings() 或者phptemplate_settings()函数。
如果一个用户以前保存过主题设置表单,那么保存过的值将通过$saved_settings参数传递给这个函数。对于添加到表单的小部件,其返回类型应该为表单API数组。
下面例子中的注释给出了更详细的解释:
<?php
// An example themes/garland/theme-settings.php file.
/**
* Implementation of THEMEHOOK_settings() function.
*
* @param $saved_settings
* array An array of saved settings for this theme.
* @return
* array A form array.
*/
function phptemplate_settings($saved_settings) {
/*
* The default values for the theme variables. Make sure $defaults exactly
* matches the $defaults in the template.php file.
*/
$defaults = array(
'garland_happy' => 1,
'garland_shoes' => 0,
);
// Merge the saved variables and their default values
$settings = array_merge($defaults, $saved_settings);
// Create the form widgets using Forms API
$form['garland_happy'] = array(
'#type' => 'checkbox',
'#title' => t('Get happy'),
'#default_value' => $settings['garland_happy'],
);
$form['garland_shoes'] = array(
'#type' => 'checkbox',
'#title' => t('Use ruby red slippers'),
'#default_value' => $settings['garland_shoes'],
);
// Return the additional form widgets
return $form;
}
?>
注意,主题作者可以使用高级表单API(auto-completion,可伸缩字段集)和JQuery javascript来创建复杂的动态的表单。
在你的drupal主题文件中获取关于设置的值
为了在drupal主题的template.php 或者.tpl.php文件中取回设置的值,可以简单的使用theme_get_setting('varname')。具体细节参看Drupal API:http://api.drupal.org/api/6/function/theme_get_setting。
例如:
<?php
$happy = theme_get_setting('garland_happy');
?>
原文:http://drupal.org/node/177868
译者:葛红儒,Think in Drupal
初始化默认值
由于我们不能保证用户是否会访问页面admin/build/themes/settings/themeName,所以我们需要为我们的自定义设置初始化默认值。
只有当我们提交过admin/build/themes/settings/themeName后,主题设置变量才被保存起来,所以在我们的template.php文件中,我们需要检查变量的设置情况。如果没被设置,我们需要将其设置为默认值。我们是这样做的,首先取出一个变量,查看它是不是为null,如果是的话,我们使用variable_set()来保存默认值,接着使用theme_get_setting('', TRUE)在Drupal内部强制刷新设置。
在你的template.php文件的顶部附近添加下面的代码:
<?php
/*
* Initialize theme settings
*/
if (is_null(theme_get_setting('garland_happy'))) { // <-- change this line
global $theme_key;
/*
* The default values for the theme variables. Make sure $defaults exactly
* matches the $defaults in the theme-settings.php file.
*/
$defaults = array( // <-- change this array
'garland_happy' => 1,
'garland_shoes' => 0,
);
// Get default theme settings.
$settings = theme_get_settings($theme_key);
// Don't save the toggle_node_info_ variables.
if (module_exists('node')) {
foreach (node_get_types() as $type => $name) {
unset($settings['toggle_node_info_' . $type]);
}
}
// Save default theme settings.
variable_set(
str_replace('/', '_', 'theme_'. $theme_key .'_settings'),
array_merge($defaults, $settings)
);
// Force refresh of Drupal internals.
theme_get_setting('', TRUE);
}
?>
注意,在前面代码中第一行的变量“garland_happy”,应被替换为你自定义主题设置中的变量名,而$defaults数据也应该是从你的theme-settings.php文件中拷贝过来的。
为新版本主题添加额外的设置
当你的主题发布了1.0版本以后,对于2.0版本,你将想要添加一些额外的自定义设置。这一过程非常直接。这里你需要注意的是第3步中的代码初始化。
原文:http://drupal.org/node/177868
译者:葛红儒, Think in Drupal