Score:1

Is it possible to override a template from the module where the original is declared

cm flag

I'd like, from within a single module, to have several 'api_field' template depending of the 'field_name' (including the default 'api-field.html.twig' )

Like api-field--country.html.twig, api-field--date.html.twig...

So i did this

/**
 * Implements hook_theme().
 */
function HOOK_block_table_theme($existing, $type, $theme, $path) {
  return [
    'api_field' => [
      'variables' => [
        'field_name' => '',
        'value' => NULL
      ],
    ]
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */

function HOOK_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($hook === 'api_field') {
    $suggestions[] = 'api_field__' . $variables['field_name'];
  }
}

But my templates ovverides are loaded only if they are in the theme, if I put them in the module they seem to be ignored by Drupal (I do place them in the "templates" directory)...

(The default template is loaded normaly from within the module)

Is this normal and if yes can it be done ?

unusedspoon avatar
aq flag
Couple of things: Is this your actual function name? `HOOK_block_table_theme`? it would need to be `yourmodulename_theme` im not sure where you got this block_table part from. Also might be worth mentioning the folder structure of your module e.g. where have you put your twig files
Thony avatar
cm flag
No I decided to make my code "generic" for some reason, but yeah the hook names are right, as a proof: once I copy paste my template override in my theme, they are loaded ! (I know this sound like a noob question but I am farely experienced with drupal...)
Mario Steinitz avatar
id flag
Well, it is a template provided by your module. So you don't want to ALTER theme suggestions, but provide some new suggestions. Your function doc block states that you're using `hook_theme_suggestions_HOOK()`. And you should be doing exactly that. Not an alter hook. Try `HOOK_theme_suggestions_api_field()`.
Thony avatar
cm flag
Well you are right about that @MarioSteinitz , I changed my code. sadly that does not fix my problem..
Score:2
cn flag

Any template override you want to place in the module folder has to be defined in hook_theme() based on the base hook:

/**
 * Implements hook_theme().
 */
function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'api_field' => [
      'variables' => [
        'field_name' => '',
        'value' => NULL
      ],
    ],
    'api_field__country' => [
      'base hook' => 'api_field',
    ],
    'api_field__date' => [
      'base hook' => 'api_field',
    ],
  ];
}
Thony avatar
cm flag
It sadly look like you are right... Too bad there are use cases like mine for using templates suggestion directly in modules, I guess typing that little extra code is okay :)
4uk4 avatar
cn flag
There is nothing preventing you from putting dynamic PHP code in the hook. This runs when the module is installed and then you could check which templates are present in the module template folder.
Thony avatar
cm flag
True, I still would have found overrides cleaner, but oh well live goes on ;)... Thx anyway!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.