Parag Kuhikar
Parag Kuhikar

Reputation: 485

How to override template file item-list.html.twig for field_slider_images in Drupal 8?

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.

Upvotes: 2

Views: 1629

Answers (1)

Rotari Radu
Rotari Radu

Reputation: 655

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

}

Upvotes: 0

Related Questions