我正在尝试为我的用户提供更改页面上使用的背景图像的功能。
背景图像列表将是一个很小的数字,不会真正改变。
我想我可以添加一些分类术语……每种背景类型一个……然后在查看页面时将一个类应用于 body 标记。
这听起来可行吗?如果可行,我将如何去做?
谢谢
山姆
最佳答案
编辑:在澄清我对问题的误解后修改答案
如果要按(节点)页面定义背景图像,那么通过分类词汇表的方法听起来是正确的方法。要使这些术语可用于 CSS,最简单的方法是将它们作为类输出/使用在 node.tpl.php 文件中,您可以在其中直接访问 $node
多变的。但在这种情况下,它们有点隐藏在生成的标记的中间,这使得正确使用它们有点困难。
为了将它们添加到 $body_classes
page.tpl.php 中的变量,您必须操作 zen_preprocess_page()
也可以添加它们,或者(更好的方法)将它们添加到您自己的模块/主题中 preprocess_page()
函数,以zen函数为例:
function yourModuleOrTheme_preprocess_page(&$vars) {
// Add classes for body element based on node taxonomy
// Is this a node page?
if ('node' == arg(0) && is_numeric(arg(1))) {
// Yes, extract wanted taxonomy term(s) and add as additional class(es)
$node = node_load(arg(1));
$background_vid = yourFuntionToGetTheBackgroundVocabularyId(); // Could be hardcoded, but better to define as variable
$terms = $node['taxonomy'][$background_vid];
foreach ($terms as $tid => $term) {
// NOTE: The following assumes that the term names can be used directly as classes.
// You might want to safeguard this against e.g. spaces or other invalid characters first.
// Check the zen_id_safe() function for an example (or just use that, if zen is always available)
$vars['body_classes'] .= ' ' . $term;
}
}
}
注意:未经测试的代码,可能包含拼写错误和其他疏忽。
( 编辑前的原始答案 - 基于对 OP 意图的误解,如果其他人也误解它,请离开她 :)
基本想法听起来是可行的,但我建议做一个小改动:
由于您希望每个用户都可以调整设置,因此您必须跳过一些环节才能允许用户使用分类术语“标记”自己。我认为只启用(核心,但可选)profile module 会容易得多并在那里配置一个“背景”字段(类型为“列表选择”)。该字段将显示在用户页面(或该页面上的单独选项卡,如果您给它一个类别),并且稍后可以很容易地从代码中获得用户选择,例如为页面模板派生一个类:
global $user;
// NOTE: The following call would be the explicit way,
// but usually the profile fields get added to the $user object
// automatically on user_load(), so you might not need to call it at all,
// extracting the values directly from the $user object instead
$profile = profile_load_profile($user);
$background = $user->profile_background
关于drupal - 根据用户的选择更改 Drupal 页面的背景图像...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2871324/