9

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

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

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.