4

I'm willing to change the user-profile-item.tpl.php template provided by the user module.

I've created a new template file. Now my question is:

  1. Where do I put the new template user-profile-item.tpl.php template file? I've created a custom module, should I place it right in the custom module's directory?
  2. Do I need anything to do so that Drupal picks up my template file instead of the default template file?

PS I got this link did not understand. Sorry I'm a newbie :(

EDIT:

I solved this using preprocessors, I figured out that I can use preprocessors easily without changing template file. I wrote following preprocessor in my module to achieve what I was looking for.

function MYMODULE_preprocess_user_profile(&$vars){
//    dsm($vars); // prints nice debugging information
    $vars['user_profile']['charity_link'] = array(
    '#markup' => l('Add Charity!', 'my/custom/link' . arg(1)),
    '#weight' => 10
    );
}
3

I had to use the following snippet to get it to work. I think it may be due to the way I set up the original template.

<?php
//  unset the original template to set yours.

// Implement hook_theme_registry_alter
function MODULENAME_theme_registry_alter(&$theme_registry) {
  $theme_registry['TEMPLATE_TO_OVERRIDE']['template'] = drupal_get_path('module', 'MODULENAME') . '/TEMPLATE_TO_OVERRIDE';
  $theme_registry['TEMPLATE_TO_OVERRIDE']['theme path'] = drupal_get_path('module', 'MODULENAME');

}
  • Here is explained with more details. Thanks! – Roger Nov 20 '15 at 11:30
1

This is easier to do in a custom theme.

As noted in the documentation, the registry keys for themes are set up in the following manner:

$theme_registry['user_profile'] = array(
  'variables' => array(
    'account' => NULL,
  ),
  'template' => 'modules/user/user-profile',
  'file' => 'modules/user/user.pages.inc',
  'type' => 'module',
  'theme path' => 'modules/user',
  'preprocess functions' => array(
    0 => 'template_preprocess',
    1 => 'template_preprocess_user_profile',
  ),
);

So basically, I believe you could simply replace theme path key with the path to your module, and your template would be used.

<?php
//  unset the original template to set yours.

// Implement hook_theme_registry_alter
function MODULENAME_theme_registry_alter(&$theme_registry) {
  $theme_registry['TEMPLATE_TO_OVERRIDE']['theme_path']  =>
    drupal_get_path('module', 'MODULENAME')) 
}
?>

Now, you would have to find the proper variable in $theme_registry, this code example is not working code. You would also probably need to copy the original preprocess function in the module since the theme path is now overridden.

There is probably an easier way of doing this, but believe me doing this in a theme is way simpler, and less chances to break existing functionality of existing modules.

In a custom theme scenario, you would just need to have your copy of the custom template in your theme and it would be good to go.

  • Thanks for helping, @stefgosselin . Eventually (after Googling a lot) I found another way, I'll post it soon here. – Shafiul Mar 21 '12 at 5:23
  • @giga would you be able to post your other way, please. Thanks! – big_smile Dec 16 '13 at 8:25
0

@stefgosselin is absolutely correct but need a little correction in code section. i am writing the exact code which worked for me.

<?php
// Implement hook_theme_registry_alter
function MODULENAME_theme_registry_alter(&$theme_registry) {
  $theme_registry['TEMPLATE_TO_OVERRIDE']['theme path']  =
    drupal_get_path('module', 'MODULENAME');
}
?>

just try to implement this code inside your custom module and it will definitely work.

0

Here is an example of mytemplate.tpl.php located in my module's templates directory overwrites toolbar.tpl.

/**
 * Implements hook_registry_alter().
 */
function lml_node_theme_registry_alter(&$theme_registry) {
  $module_path = drupal_get_path('module', 'my_module');
  // Override the default template with our own.
  if (isset($theme_registry['toolbar'])) {
    $theme_registry['toolbar']['theme path'] = $module_path;
    $theme_registry['toolbar']['path'] = $module_path . '/templates';
    $theme_registry['toolbar']['template'] = 'mytemplate';
  }
}

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.