根据用户的选择更改Drupal页面的背景图像......?

Sam*_*mbo 5 background drupal taxonomy

我正在尝试为我的用户提供更改页面上使用的背景图像的功能.

背景图像列表将是一个小数字,不会真正改变.

我想我可以添加一些分类术语...每个背景类型一个...然后在查看页面时将一个类应用于body标签.

这听起来是否可行,如果是这样,我该如何去做呢?

谢谢

山姆

Hen*_*pel 1

编辑:澄清我对问题的误解后修改答案

如果要为每个(节点)页面定义背景图像,那么通过分类词汇表的方法听起来是正确的方法。要使这些术语可用于 CSS,最简单的方法是将它们作为 node.tpl.php 文件中的类输出/使用,您可以在其中直接访问变量$node。但在这种情况下,它们在某种程度上被隐藏在生成的标记的中间,这使得正确使用它们有点困难。

为了将它们添加到$body_classespage.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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:未经测试的代码可能包含拼写错误和其他疏忽。


编辑前的原始答案- 基于对OP意图的误解,让她以防其他人也误解它:)
基本想法听起来可行,但我建议做一个小的变化:

由于您希望每个用户都可以调整设置,因此您必须跳过一些步骤才能允许用户使用分类术语“标记”自己。我认为启用(核心但可选)配置文件模块并在那里配置“背景”字段(类型为“列表选择”)会容易得多。该字段将显示在用户页面上(或者该页面上的单独选项卡,如果您给它一个类别),并且稍后可以很容易地从代码中获得用户选择,例如为页面模板派生一个类:

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
Run Code Online (Sandbox Code Playgroud)