0

I have the following preprocess function and was wondering what I have wrong here as its not working.

This is just to grab a fields value and insert it into the body classes.

/**
 * Implements hook_preprocess_html().
 */
function cotyhouse_preprocess_html(&$vars){
  $node = menu_get_object('node');

  // Check if on an article node page
  if ($node->type == 'article') {
    // Get field values
    if (field_get_items('node', $node, 'field_title_color') == 'black') {
      $vars['classes_array'][] = drupal_html_class('tblk');
    }   
    if (field_get_items('node', $node, 'field_title_color') == 'white') {
      $vars['classes_array'][] = drupal_html_class('twhi');
    }   
    if (field_get_items('node', $node, 'field_title_color') == 'white_shadow') {
      $vars['classes_array'][] = drupal_html_class('tsha');
    }   
  }    
}
1
  • 1
    drupal_field_get_items returns the field item and not the value. Commented Oct 18, 2014 at 16:10

1 Answer 1

1

field_get_items return an array of field item so you need to do something like :

/**
 * Implements hook_preprocess_html().
 */
function cotyhouse_preprocess_html(&$vars){
  $node = menu_get_object('node');

  // Check if on an article node page
  if ($node->type == 'article') {

    // get field_title_color value
    $color = NULL;
    $field_items = field_get_items('node', $node, 'field_title_color');
    if ($field_items) {
      $first_item = reset($field_items);
      $color = $first_item['value'];
    }

    // Set class corresponding to color
    if ($color == 'black') {
      $vars['classes_array'][] = drupal_html_class('tblk');
    }   
    if ($color == 'white') {
      $vars['classes_array'][] = drupal_html_class('twhi');
    }   
    if ($color == 'white_shadow') {
      $vars['classes_array'][] = drupal_html_class('tsha');
    }   
  }    
}

Your Answer

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

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