0

I need to customize the image.html.twig template. I have a new content type with an image field (field_tile_image). How can I overwrite the default image template only for one block?

I tried different naming but it's still using image.html.twig. There aren't other name suggestions.

3
  • 2
    What kind of modifications do you want to apply? The thing is, it will be hard to do what you want. You'll probably want to add a different viewmode for your image. To do so, you'll need to work with media instead of the basic image field.
    – JFC
    Feb 21, 2022 at 17:36
  • @JFK I need to remove image width, height attributes also add custom HTML tags, but only for this image field without touching the original one.
    – Klapsius
    Feb 22, 2022 at 8:53
  • Welcome to Drupal Answers! Since the question is about changing the template file used from an image field, but only for a block, the question should make clearer the relation between the content type and the block, how that block is created, and what exactly it outputs. At least there should be more information that allow to "identify" that block.
    – apaderno
    Feb 22, 2022 at 19:56

1 Answer 1

1

The image.html.twig file does not have any names suggestions by default. Then you have to add it yourself. You can do it using hook_theme_suggestions_alter.

Also, here's an example of this hook usage:

/**
 * Implements hook_theme_suggestions_alter().
 */
function MY_THEME_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($hook == 'taxonomy_term') {
    $term = $variables["elements"]["#taxonomy_term"];
    $vid = $term->get('vid')->target_id;
    $view_mode = $variables["elements"]["#view_mode"];
    
    $suggestions[] = $hook . '__' . $view_mode;
    $suggestions[] = $hook . '__' . $vid . '__' . $view_mode;
  }
}

You can read more about it here:
https://www.drupal.org/docs/theming-drupal/twig-in-drupal/working-with-twig-templates
and here:
https://www.drupal.org/docs/theming-drupal/twig-in-drupal/twig-template-naming-conventions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.