0

I have a field in a node type called body_styles.

I need to modify html.tpl.php to get the value of body_styles to add it to the classes of the body tag.

I tried with dpm($node->content['body_styles']['#value']); but I get nothing. I think it is because $node is not available in html.tpl.php.

how do I get a node field value in a html.tpl.php template file?

@svetlio: almost done:

The class is correctly added to $variables['classes_array'] but if I inspect the body (see my capture below the code) tag is not there

    function omega_subtheme_2_preprocess_html(&$variables, $hook) {
    if (arg(0) == 'node' && is_numeric(arg(1))) {
        global $language;
        $node = entity_load_unchanged('node', arg(1));

        dpm($node -> field_body_class['und'][0]['value']);

        if (isset($node -> field_body_class['und'][0]['value']) && $node -> field_body_class['und'][0]['value']){
            $variables['classes_array'][] = $node -> field_body_class['und'][0]['value'];
            dpm($variables['classes_array']);
        }
  }

enter image description here

  • Have you tried clearing your cache? – aendrew Aug 1 '12 at 13:04
  • I clear all cache several times, not working... – chefnelone Aug 1 '12 at 14:13
1

Another way - without edit template and adding code in template.php

function THEME_preprocess_html(&$variables, $hook) {
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      $node = entity_load_unchanged('node', arg(1));

      if (isset($node -> field_FIELD_NAME['und'][0]['value']) && $node -> field_FIELD_NAME['und'][0]['value']) {
        $variables['classes_array'][] = $node -> field_FIELD_NAME['und'][0]['value'];
      }
    }
  }

replace THEME with your theme_name and FIELD_NAME with your field_name, clear the cache

edit: I change "$node = entity_load('node', arg(1));" to $node = "entity_load_unchanged('node', arg(1));"

  • I use friendly url and it is a multilingual site then the urls has appended the Path prefix (en, es, fr). then the function get the arguments in a wrong way: arg(1) and arg(0) – chefnelone Aug 1 '12 at 11:02
  • add "global $language;" before "$node = entity_load_unchanged('node', arg(1));" and replace all 'und' with "$language -> language" without the quotes – svetlio Aug 1 '12 at 12:05
  • almost there. I need to leave 'und' to get it to work. The class is correctly added to $variables['classes_array'] but if I inspect the body tag is not there. Please see the function at the bottom of my question. – chefnelone Aug 1 '12 at 12:45
  • must to clear cache after each change in template.php before refreshing page, maybe twice – svetlio Aug 1 '12 at 13:50
  • 1
    I read the doc for preprocess function with omega. You are right, it is different. I can't get it working though, but this out the scope of this question – chefnelone Aug 2 '12 at 7:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.