How to add 'first' and 'last' class names in addtion to 'odd'/ 'even' for field collection items in preprocess functions?
2 Answers
You would normally do this in MYTHEME_preprocess_field_collection_item(), but field collection items don't have their own preprocess. Fortunately, they're entities, so you can use entity preprocess to create your own field collection preprocess function:
/** * Implements template_preprocess_entity(). */ function MYTHEME_preprocess_entity(&$variables, $hook) { $function = 'MYTHEME_preprocess_' . $variables['entity_type']; if (function_exists($function)) { $function($variables, $hook); } } /** * Field Collection-specific implementation of template_preprocess_entity(). */ function MYTHEME_preprocess_field_collection_item(&$variables) { $variables['classes_array'][] = 'your-class-here'; // Plus whatever other preprocessing you want to do. } In Drupal 8 the preprocess function exists without having to add one:
/** * Implements hook_preprocess_field_collection_item(). */ function mymodule_preprocess_field_collection_item(array &$vars, $hook) { //dpm($vars); }
hook_preprocess_field(&$vars)