9

How to add 'first' and 'last' class names in addtion to 'odd'/ 'even' for field collection items in preprocess functions?

2
  • 1
    untested: dropbucket.org/node/764 Commented Oct 25, 2014 at 20:02
  • Yepp, have a look inside hook_preprocess_field(&$vars) Commented Mar 16, 2015 at 16:49

2 Answers 2

22

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. } 
0
2

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); } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.