0

I have a content type called "article" and I have a field called "text color" and I want to add this text color field's value as a class to either the body or content container and change the text color of the content.

  • Which preprocess function should i go about using?
  • Is it possible to just do an inline style using the text color field's value so i don't have to also edit my css file too?
  • Or is there another elegant way of doing this without using a preprocess function?
1

You can add the class to the body from the node field value in hook_preprocess_html

Something like:

/**
 * Implements hook_preprocess_html
 */
function THEME_preprocess_html(&$vars){
  // Check if on an article node page
  if($node = menu_get_object('node') && $node->type == 'article'){
    // Get field values
    if($text_color = field_get_items('node', $node, 'field_text_color')){
      // Add first text_color value to body class
      $vars['classes_array'][] = drupal_html_class(reset($text_color));
    }
  }

}

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.