1

How can I find the 'base hook' to override a template file using a custom module?

'node' and 'user' are pretty straightforward, but others can be a little more elusive.

Example code...

MYMODULE.module:

/**
 ** Implements hook_theme().
 **/
function MYMODULE_theme($existing, $type, $theme, $path) {
  return [
    'username' => [
      'template' => 'username',
      'base hook' => 'how do you get this?',
    ],
  ];
}
  • You don't need template if the template is named the same as the theme. – leymannx Dec 2 '18 at 7:20
5

Look into the *.module file where the base hook is defined, in this case the User module. The base hook is the key of the template definition:

user.module:

/**
 * Implements hook_theme().
 */

function user_theme() {
  return [
    ... 
    'username' => [
      'variables' => ['account' => NULL, 'attributes' => [], 'link_options' => []],
    ],
  ];
}

Then in your *.module file

mymodule.module

/**
 * Implements hook_theme().
 */
function mymodule_theme() {
  return [
    'username__mymodule_template' => [
      'base hook' => 'username',
    ],
  ];
}

Start the name of your own hook with the base hook and double underscores.

  • Want to cross-reference this question: drupal.stackexchange.com/questions/200602/…. Note, for the above template file to apply you will need to alter/add a theme suggestion so this file is looked for. Example hook_theme_suggestions_username_alter(array &$suggestions, array $variables) – Prestosaurus Dec 3 '18 at 0:31

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.