Score:0

image.html.twig naming sugestions

mx flag

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.

mx flag
JFK
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.
Klapsius avatar
mx flag
@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.
apaderno avatar
us flag
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.
Score:1
ru flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.