0

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.

1

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'
    ],
  ];
}
-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.

3
  • 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 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 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 at 12:37

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.