views:

283

answers:

1

Hi

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

+1  A: 

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
Henrik Opel
It isn't a setting that I would set per user...its intended to be per page. The user would select a background type from a list box when they created a new page and the selected background will always be the same for that individual page.I'm using a sub-theme of Zen which already outputs '$body_classes' into the body tag of all pages. What I am thinking to do is to output the taxonomy term along with this.
Sambo
@Sambo: I see, sorry for the misunderstanding. I edited my answer accordingly.
Henrik Opel