0

我想覆盖项目列表模板文件core/themes/classy/templates/dataset/item-list.html.twig以分别列出它们的多个字段值的字段field_slider_imagesfield_blog_tags 。

我在视图中选择了“无序列表”。

请检查所附图片。

显示类型:field_slider_images 的无序列表

我创建了以下文件:

  1. 项目列表--字段-博客-tags.html.twig
  2. 项目列表--字段-滑块-images.html.twig

但是,这不是针对字段列表呈现的。

当我创建了item-list.html.twig之后,只有它可以访问。

但是,这两个字段都有不同的数据来设置样式,我无法获取当前字段名称,该名称正在将其数据加载到item-list.html.twig中。

4

1 回答 1

0

对此进行了简要介绍,“项目列表”似乎没有建议,这很不幸。

在这种情况下,有两种选择:

  1. 创建您自己的建议,这将完全满足您的需求。你必须做这样的事情:

/

/*add new variable to theme with suggestion name*/
function hook_theme_registry_alter(&$theme_registry) {
  $theme_registry['item_list']['variables']['suggestion'] = '';
}

//send a value to newly added variable to use it build the suggestion
function hook_ENTITY_TYPE_view(array &$build, $entity, $display, $view_mode) {
    //add condition here if field exists or whatever, do the same for other field
    $build['field_slider_images']['#suggestion'] = 'field_slider_images';

}

//use newly added variable to build suggestion
function hook_theme_suggestions_THEME_HOOK(array $variables) {//THEME_HOOK=item_list

  $suggestions = array();
  if(isset($variables['suggestion'])){
    $suggestions[] = 'item_list__' . $variables['suggestion'];
  }

  return $suggestions;
}

现在您应该可以使用 item-list--field-slider-images.html.twig

  1. 第二种选择是做核心中其他人所做的事情:使用新主题

    function hook_ENTITY_TYPE_view(array &$build, $entity, $display, $view_mode) { //add condition here if field exists or whatever, do the same for other field $build['field_slider_images']['#theme'] = array( 'item_list', 'item_list__field_slider_images', );

}

于 2016-12-29T18:22:02.047 回答