2

I want to override the item listing template file core/themes/classy/templates/dataset/item-list.html.twig for listing the fields field_slider_images as well as field_blog_tags respectively of their's multiple values of the field.

I have selected "Unordered List" in the view.

Please do check the attached image.

Display Type : Unordered List for field_slider_images

I have created following files :

  1. item-list--field-blog-tags.html.twig
  2. item-list--field-slider-images.html.twig

But, this is not rendered for the listing of the fields.

When I have created item-list.html.twig then only it will access.

However, both fields have different data to style and I am not able to get the current field name which is loading it's data in item-list.html.twig.

Parag Kuhikar
  • 485
  • 2
  • 6
  • 17

1 Answers1

0

Had a brief look at this and it doesn't seem that 'item-list' to have suggestions, which is quite unfortunate.

In this situation there are two options:

  1. Create your own suggestion which would accomplish exactly what you need. You'll have to do something like this:

/

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

Now you should be able to use item-list--field-slider-images.html.twig

  1. Second option is to do what others in core did: use a new theme

    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', );

}

Rotari Radu
  • 645
  • 6
  • 13
  • Thank you so muche. However, it is applying to all the fields of the node page. In this page, I have used field_slider_images as well as field_blog_tags. If I apply function hook_node_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'; } then It is also applicable for both fields. For both fields, it have item-list--field_slider_images.tpl.php. Please help – Parag Kuhikar Dec 30 '16 at 07:56
  • I have created two blocks. One block is rendering field_slider_images and the other is rendering field_blog_tags. After that, I have attached that in block region of the node view. Please help how to create that item_list for these two fields with respect to two different blocks. – Parag Kuhikar Dec 30 '16 at 07:58
  • I don't see how $build['field_slider_images']['#suggestion'] = 'field_slider_images'; applies for both fields you just have to add next the suggestion for the other field like $build['field_blog_tags']['#suggestion'] = 'field_blog_tags'; clear registry and item-list--field-slider-images.html.twig should be available – Rotari Radu Dec 30 '16 at 16:01
  • I am not able to get fields in $build of hook_ENTITY_TYPE_view. – Parag Kuhikar Jan 03 '17 at 04:48