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.

0

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.

  • 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
0

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

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.