0

Working on a clients drupal 7 site, I have a content type that is promoted to the front page. The content type has a list field, I want to use the value of that field to add a class to the wrapper div on the node teasers populated to the front page. For example the html output would be something like this:

<div class="field_value">
content
</div>

I'd prefer not to have to use a view or nodequeue to do this as it's easier on the client administering their site to simply promote items to front page.

I've been trying to add the class in the node.tpl.php file but for some reason I just can't get it to work. I've tried creating a preprocess variable in the template.php file but all I get in the output is: div class="Array".

Does anyone have any ideas on why I'm just getting the word "Array" output? or any ideas on how to solve my problem?

Thanks

1

Something like this should do it:

function MYTHEME_preprocess_node(&$vars) {
  if ($vars['node'] == 'some_type') {
    $field_items = field_get_items('node', $vars['node'], 'field_name');
    $first_item = array_shift($field_items);

    $value = $first_item['value'];

    // If you want to write <div class="<?php echo $extra_class; ?>">... in your template file:
    $vars['extra_class'] = $value;

    // Or if you want to add this to the classes on the main node <div>:
    $vars['classes_array'][] = $value;
  }
}
0

You should do in the template.php file.

You should look at function my_theme_preprocess_node(){ // custom code. }

You will be able to add the class to the body wrapper there. You should take a look at: - http://api.drupal.org/api/drupal/modules!node!node.module/function/template_preprocess_node/7 - http://drupal.org/node/1170752

Also when you should know how to use http://drupal.org/project/devel module and http://drupal.org/project/devel_themer module when you build themes.

Oskar

  • Uh, this doesn't answer my question at all... Devel modules have nothing to do with what I'm trying to do. I need to get the value of a custom field and add it to a module. – JamiePatt Sep 9 '12 at 10:48

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.