3

I have a custom module where I am trying to use custom twig templates based on the user view mode. The templates files I am trying to add to my theme are:

  • user.html.twig
  • user--compact.html.twig
  • user--token.html.twig

These reside in a folder called MYMODULE/templates/.

This is my current module code:

module_name.module:

/**
 * Implements hook_theme().
 */
function MYMODULE_theme($existing, $type, $theme, $path) {

  return [
    'user' => [
      'template'  => 'user',
      'base hook' => 'user',
    ],
    'user__compact' => [
      'template'  => 'user--compact',
      'base hook' => 'user',
    ],
    'user__token' => [
      'template'  => 'user--token',
      'base hook' => 'user',
    ],
  ];

}
  • Could you edit your question and clarify what exactly you are trying to achieve? – leymannx Sep 22 '18 at 17:59
  • 1
    I've added to my question, hopefully that helps clarify. – Prestosaurus Sep 22 '18 at 18:09
  • If it helps, when I dpm($existing); the only base hooks are field, field_multiple_value_form, and block – Prestosaurus Sep 22 '18 at 18:15
3

The hook_theme_suggestions_HOOK_alter() is more suited to alter existing suggestions.

In this case, if you are just adding a suggestion, not altering the pre-existing ones, you could use the hook_theme_suggestions_HOOK.

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function MYMODULE_theme_suggestions_user(array $variables) {
  return [
    'user__' . $variables['elements']['#view_mode']
  ];
}

/**
 * Implements hook_theme().
 */
function MYMODULE_theme($existing, $type, $theme, $path) {

  return [
    'user' => [
      'template'  => 'user',
      'base hook' => 'user',
    ],
    'user__compact' => [
      'template'  => 'user--compact',
      'base hook' => 'user',
    ],
    'user__token' => [
      'template'  => 'user--token',
      'base hook' => 'user',
    ],
  ];
}
  • Hey Cesar, you first mentioned the alter hook but then used the bare suggestion hook in the sample. I switched the snippet to use the alter hook. OP added hook_theme. I hope that's OK for you, if not please reroll the edits back to an older revision. Nice answer. :) – leymannx Sep 23 '18 at 2:16
  • 1
    Hi @leymannx, the hook_theme part is not really needed, let me just delete that bit and explain it. – Cesar Moore Sep 23 '18 at 2:18
  • 1
    Seems OP just clarified they want to have the templates living in their module's templates folder, not in the theme. I guess that changes the initial situation. – leymannx Sep 23 '18 at 2:30
2

The fix was ensuring to add the theme suggestions via HOOK_theme_suggestions_HOOK_alter(), as well as switching one template from user to user__full. Below is the updated module code:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function MYMODULE_theme_suggestions_user_alter(&$suggestions, $variables) {

  $view_mode = $variables['elements']['#view_mode'];
  $suggestions[] = 'user__' . $view_mode;
}

/**
 * Implements hook_theme().
 */
function MYMODULE_theme($existing, $type, $theme, $path) {

  return [
    'user__full'    => [
      'template'  => 'user--full',
      'base hook' => 'user',
    ],
    'user__compact' => [
      'template'  => 'user--compact',
      'base hook' => 'user',
    ],
    'user__token'   => [
      'template'  => 'user--token',
      'base hook' => 'user',
    ],
  ];
}

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.