预处理字段收集项以添加名字和姓氏的正确方法是什么?


Answers:


22

通常,您可以在MYTHEME_preprocess_field_collection_item()中执行此操作,但是字段收集项没有自己的预处理。幸运的是,它们是实体,因此您可以使用实体预处理来创建自己的字段集合预处理函数:

/**
 * 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

Drupal 8中,预处理功能存在而无需添加一个:

/**
 * Implements hook_preprocess_field_collection_item().
 */
function mymodule_preprocess_field_collection_item(array &$vars, $hook) {
  //dpm($vars);
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.