1

I'm trying to create a suggestion like image--NODE-TYPE--VIEW-MODE, but in mytheme_theme_suggestions_image_alter() $vars i haven't node related info enter image description here

thanks for help

1
  • 1
    Welcome to Drupal Answers! When asking questions, please cut-and-paste your code and then mark it with code tags instead of taking a screenshot and uploading that. Code pasted as text is searchable and helps people find your question when they search. Jan 21, 2021 at 2:09

1 Answer 1

2

In the hook_theme_suggestions_HOOK_alter the $variables contain only informations about the theme of entity in this case theme image:
From documentation:

array $variables: An array of variables passed to the theme hook. Note that this hook is invoked before any preprocessing.

if you look at image_theme you can find all variables passed uri, title ...

So to access node related info you can use route params like the following.

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function THEME_theme_suggestions_image_alter(array &$suggestions, array $variables) {
  $node = \Drupal::routeMatch()->getParameter('node');
  $suggestions [] = "image__" . $node->bundle();
}

Note: you can't access to View mode in this hook because it's not in the $variables and also the current view mode is not stored in the node object

Take a look at this issue Make the current view mode available on the node object

1
  • Will this work when you're displaying a node somewhere other than the canonical node route?
    – sonfd
    Jan 21, 2021 at 13:10

Your Answer

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

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