I have seen many questions like this ones and I have tried all the solutions…
Here is my setup:
1-I have a custom entity with the id defined as follow:
* @ContentEntityType( * id = "bank_operation", * label = @Translation("Bank Operation"),
2- My hook_theme()
:
function bank_operation_theme() { return [ 'bank_operation' => [ 'render element' => 'elements', ], ]; }
3- My hook_theme_suggestions_HOOK()
function bank_operation_theme_suggestions_bank_operation(array $ variables) { $ suggestions = []; $ sanitized_view_mode = strtr($ variables['elements']['#view_mode'], '.', '_'); $ suggestions[] = 'bank_operation__' . $ sanitized_view_mode; return $ suggestions; }
4- My Route to display the entity with a "receipt" view mode
entity.bank_operation.receipt: path: '/bank_operation/receipt/{bank_operation}' defaults: _entity_view: bank_operation.receipt _title: 'Receipt' requirements: _entity_access: 'bank_operation.view'
5-My twig files:
modules/custom/bank_operation/templates/bank-operation.html.twig modules/custom/bank_operation/templates/bank-operation--receipt.html.twig
Result: Everything works fine :
–receipt
view mode is used
-suggestions seems to be correctly added:
<!-- THEME HOOK: 'bank_operation' --> <!-- FILE NAME SUGGESTIONS: x bank-operation.html.twig * bank-operation--receipt.html.twig x bank-operation.html.twig --> <!-- BEGIN OUTPUT from 'modules/custom/bank_operation/templates/bank-operation.html.twig' -->
Except that bank-operation--receipt.html.twig
is not called
I have tried all combinations of file name/suggestions/base hook name/… without any success
I have added all the possible twig file name in my templates
folder (with or without dash)
I have the feeling that the problem is coming from the fact that the ID of my entity have an underscore in its name…