0

I'm struggling to make template suggestions like IMAGE--BLOCK-TYPE or IMAGE--FIELD-TYPE. I have three different block types(image1, image2, image3) with one image field(field_image_1, field_image_2 and field_image_3). And I don't know how to get suggestion like

image--block_image1.html.twig
image--block_image2.html.twig
image--block_image3.html.twig

or

image--field_image_1.html.twig
image--field_image_2.html.twig
image--field_image_3.html.twig

Is there any way to pass variable between the two hooks below?

  function full_nature_theme_suggestions_block_alter(array &$suggestions, array $variables) {

    global $block_id;

    $block_id = $variables['elements']['#id'];

  }

and

  function full_nature_theme_suggestions_image_alter(array &$suggestions, array $variables) {

    $suggestions[] = 'image__'.$block_id;

  }

Please help!

2 Answers 2

0

Solution is here

https://www.drupal.org/forum/support/theme-development/2021-02-23/how-to-create-template-suggestion-for-image-like-image#comment-14017195

It is alternative solution but it works with this module

https://www.drupal.org/project/image_class

0

This is right solution but it works only with this module https://www.drupal.org/project/image_class

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */

function full_nature_theme_suggestions_image_alter(array &$suggestions, array $variables) {
  $image = $variables['attributes']['class'][0];
  $suggestions[] = 'image__'.$image;
}
1
  • Could you please put both your answers into one, highlight the code and summarize the forum post you are referring to? Many thanks
    – leymannx
    Mar 4, 2021 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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