4

I add template suggestion in my discount module:

function discount_theme_suggestions_field_multiple_value_form_alter(array &$suggestions, array $variables) {
    if ($variables['element']['#field_name'] == 'user_discount'){
      $suggestions[] = 'field_user_discount';
      kint($suggestions);
    }
}

And I can see template suggestion on my admin form: enter image description here

However I added template field-user-discount.html.twig (I don't place it here as it simply the copy of classy one with change of order) in discount module templates folder and it is not being picked up, it still picks up the classy one. I cleared all the caches, both from drush and site itself. I also tried to add this template to theme templates, it is not being picked up there as well. Other templates from both theme and discount modules are being picked up.

How can Drupal pick it up? It is the field on admin pages.

2 Answers 2

4

After you add your theme suggestion:

function discount_theme_suggestions_field_multiple_value_form_alter(array &$suggestions, array $variables) {
    if ($variables['element']['#field_name'] == 'user_discount'){
      $suggestions[] = 'field_user_discount';
    }
}

You need to tell Drupal in this hook you should pick my template by implementing hook_theme like the following.

/**
 * Implements hook_theme().
 */
function MODULE_theme($existing, $type, $theme, $path) {
  return [
    'field_user_discount' => [
      'base hook' => 'field_multiple_value_form'
    ],
  ];
}

Now just put your template in you templates module folder clear cache and it will be picked.

Note: if you want to use a different name from the suggested one (field-user-discount) of your template like field-user-discount-something.html.twig just tell the hook_theme the name of template like:

 /**
 * Implements hook_theme().
 */
function MODULE_theme($existing, $type, $theme, $path) {
  return [
    'field_user_discount' => [
      'base hook' => 'field_multiple_value_form',
      'template' => 'field-user-discount-something'
    ],
  ];
}
2
  • Your solution works if I put the template into the themes template/ folder. But if I do it put into the myModules template/ folder: fail. It doesn't work. Could you explain, how I can get it work with putting the template file into the myModules template/ folder? Apr 12 at 6:21
  • In the hook_theme add the path key under template and specify the path of your template
    – berramou
    Apr 12 at 15:50
-1

I added template field-user-discount.html.twig ... in discount module templates folder

That's the wrong place to put it. You need to put this in your theme, not in the module's templates folder.

4
  • I also put this on my theme and it didn't work, it was written in my original post ;) probably because I do not overwrite admin theme. Anyway I just figured out, that I will try to point templates folder with theme_registry_alter
    – rukya
    Jan 14, 2021 at 9:56
  • Doesn't deserve a downvote. If berramou's answer worked for you, then that means my answer is correct. berramou shows how to add a hook_theme() to your module to tell your module that it can find the twig template in your module's templates directory INSTEAD of the default location in the theme. If that works, then the reason your template was not found was because you had it in the wrong place, despite what you think.
    – anonymous
    Feb 15, 2021 at 22:55
  • I didn't mean to hurt your feeling. In my opinion your answer showed that you haven't read my question and the details I provided, things I have tried, so it was just a waste of time, mine & yours. I will take the back the downvote, but only because you care about it so much, I still find this answer just meaningless. Besides, why do you care so much about stuff like that? If I did, I imagine, that my life would be a hell, think about this. All the best! Edit: but from what I see I cannot take back the downvote unless you edit an answer, so let me know, once you did that
    – rukya
    Feb 16, 2021 at 12:37
  • @rukya: this answer is NOT meaningless just because the practice to put the template files into the THEMES template folder seems to be the best practice. If you don't think so, could you explain, please, how can I get it work with putting the template file into the myModules template/ folder. The standard way it doesn't work. Thanks. Apr 12 at 6:26

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.