Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 855 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a class to "body"

#1
How can I modify or pre-process the `<body>` tag to add the class body? I don't want to create a whole html.tpl.php just to add a class.
Reply

#2
In your theme's `template.php` file use the `preprocess_html` hook:

function mytheme_preprocess_html(&$vars) {
$vars['classes_array'][] = 'new-class';
}

Remember to clear the caches once you've implemented the hook or Drupal won't pick it up.
Reply

#3
[The documentation for the html.tpl.php template][1] documents the `$classes` variables as *String of classes that can be used to style contextually through CSS.*. If you look at the code for the template, this variable is used in the class attributes of the produced body element:

<body class="<?php print $classes; ?>" <?php print $attributes;?>>

The `$classes` variables is actually already set by [`template_process()`][2] for any template file and build from the content of the `$classes_array` variable.

So to add a class to the body of your page, you should add this class to the `$classes_array` value from your theme (or module)'s implementation of `hook_preprocess_html()`:

function THEME_preprocess_html(&$variables) {
$variables['classes_array'][] = 'new-class';
}

Since this is the _core_ defined template and process function, any well-behaving theme should re-use the same variables.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
I had to use different array keys in the same hook to make it work:

function THEME_preprocess_html(&$vars) {
$vars['attributes_array']['class'][] = 'foo2';
}
Reply

#5
I applied this technique on a site that someone else built. It didn't work at first but then dug deeper and found that the $classes variable was not being output in the tpl file. So if it's not working, check that.
Reply

#6
The answer appears to depend on context. Here's what I've found via trial-and-error:

If your hook_preprocess_html() is in a **module**, use $vars['classes_array'][].

If it's in a **theme**, use $vars['attributes_array']['class'][].
Reply

#7
The [Context][1] module allows you to add a class to the body tag as well.

This can be useful if you need the class to be added under certain conditions.

You find this options under the reaction "Theme HTML" :

![Theme HTML option in Context UI][2]

[1]:

[To see links please register here]

[2]:
Reply

#8
[Common Body Class][1] module provide users to add classes to any page through the an interface.
The interface has options to select multiple user roles as well as pages where the class can be rendered.

![Example][2]


[1]:

[To see links please register here]

[2]:
Reply

#9
For Drupal 7 install [

[To see links please register here]

][1]. It will help you to add seperate classes for each node in body tag


[1]:

[To see links please register here]

Reply

#10
You can check "https://www.drupal.org/project/page_specific_class" to add class to body tag of any page
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through