1

I'm trying to make one content type that can reuse a jQuery-gallery with different styles depending on what page you're on.

So i made one field called field_CSS where I put the CSS. However for performance reasons (and for the sake of cleaning up the code) I want to put it in the HEAD-section.

The content in the head section is rendered in the html.php.tpl-filde and the field is in the content type of the specific node.

I've tried <?php print render($content['field_CSS']) ?> <?php print render($page['field_CSS']) ?> <?php print $node->field_CSS[0]['view']; ?> and a lot of other variants. Anyone who knows what to write to make it show up in the html.tpl.php file?

The field only contains pure CSS that now is printed as inline-code.

EDIT: Clive post works flawlessly. Just don't forget to fix the field theming so you don't get a div in the css-section.

1 Answer 1

3

The node is not usually available in html.tpl.php so you'll need to get your field content manually in a preprocess function. Put something like this in your theme's template file:

function MYTHEME_preprocess_html(&$vars) {
  $node = menu_get_object();

  if ($node && isset($node->nid)) {
    $node = node_load($node->nid);

    node_build_content($node);

    $vars['extra_css'] = render($node->content['field_CSS']);
  } 
}

Then you'll have the variable $extra_css in html.tpl.php which will contain your rendered field. You'll need to flush the caches once you've implemented the preprocess function and replace MYTHEME with the name of your theme.

Hope that helps

Sign up to request clarification or add additional context in comments.

5 Comments

So, I added the code to template.php and I added the $extra_css in the html.tpl.php-file. html.tpl.php looks like <?php print $extra_css; ?> And the content (not css right now, just text) does not show up on the page. However, the message "var extra_css is not defined" has gone so it should be visible. (Yes, i flushed cache etc) Thanks for such a quick answer
My bad I forgot how node_build_content() works, I'll correct the code in the post above
Disregarding the potential of sounding totally gay i must say that i absolutely love you dude. For the others reading this: Don't forget to change the field display options in field--field_FIELDNAME.tpl.php in your themefolder. Do you have the link to a good tutorial for these kinds of things?
Glad it helped :) It's a bit tricky to think of specific tutorials because this method uses a few different Drupal disciplines, there's a good About overriding themeable output page which explains how the theme lifecycle works and how you can pass variables to a theme. How you get those variables in the first place is a different story though, menu_get_object() will always get you the node object if you're on a node page, armed with that you should be able to get a good grounding
Thanks a lot, I mean a fully functional fix (complete with code and all) within less than 30 min is better than premium support at most companies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.