How to set all values of a field as body classes I want users to easily modify the layout of a node by selecting options in a 'Layout' field that the node contains. The field is a entityreference field referencing taxonomy terms. 
(For example, the user can select term 'hide-images' and the body class 'hide-images' will be used in the css to hide images.)

I have found this code that sets the first value of a field as body class. 
But how can I add all field values (taxonomy terms) as body classes?

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

}
Instead of reinventing the wheel, you could take a look at Display Suite.  DS has this functionality built in.

DS comes with a sub-module (DS extras) that has a feature that allows users to select a style right from the node edit page.

After enabling ds, ds_ui, ds_extras you can enable /admin/structure/ds/list/extras -> Other Tab -> View mode per node to use the feature.

Description of the feature:


  Change view modes for individual nodes. A new tab 'Display settings'
  will appear on the content create form. You can also pass the name of
  a view mode through the URL, eg node/x?v=full. If you install the Page
  manager module and override the node view, Page manager will win.


Update: If you still want to do it the way you started, you can just change this bit to use a foreach:

Change:

if($term = field_get_items('node', $node, 'field_layout')){
  $vars['classes_array'][] = drupal_html_class(reset($term));
}


To:

if ($term = field_get_items('node', $node, 'field_layout')) {
  foreach ($term as $item) {
    $vars['classes_array'][] = drupal_html_class($item['taxonomy_term']-&gt;name);
  }
}