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

发布于 2024-09-02 02:16:43 字数 169 浏览 6 评论 0原文

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

背景图像列表的数量很少,不会真正改变。

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

这听起来可行吗?如果可行,我将如何去做?

谢谢萨姆

I'm trying to give my users the functionality to change what the background image used on a page is.

The list of background images will be a small number that won't really change.

I thought I could add a few Taxonomy terms...one for each background type...then apply a class to the body tag when the page is viewed.

Does this sound feasible and if so how would I go about doing it?

Thanks

Sam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2024-09-09 02:16:43

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

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

为了将它们添加到 page.tpl.php 中的 $body_classes 变量中,您还必须操作 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意图的误解,离开她以防其他人也误解它:)
基本想法听起来可行,但我建议稍作修改:

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

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

EDIT: revised answer after clarification of my misunderstanding of the question

If the background image is to be defined per (node) page, your approach via a taxonomy vocabulary sounds like the right way to go. To make the terms available for CSS, the easiest way would be to just output/use them as classes in the node.tpl.php file(s), where you have direct access to the $node variable. But in that case, they are somewhat buried in the middle of the resulting markup, which makes it a bit difficult to use them properly.

In order to add them to the $body_classes variable in the page.tpl.php, you'd have to either manipulate the zen_preprocess_page() function to add them as well, or (better approach) add them to your own modules/themes preprocess_page() function, using the zen function as an example:

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;
    }
  }
}

NOTE: Untested code, might contain typos and other oversights.


(Original answer before edit - based on a misunderstanding of the OPs intent, left her in case other misunderstand it as well :)
The basic idea sounds feasible, but I'd suggest a minor variation:

Since you want the setting to be adjustable per user, you would have to jump through some hoops to allow users to 'tag' themselves with a taxonomy term. I think it would be far easier to just enable the (core, but optional) profile module and configure a 'background' field there (with type 'list selection'). The field will show up on the user page (or a separate tab on that page, if you give it a category), and the user selection will be available from code later on quite easily, e.g. to derive a class for the page template:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文