1

I've created my own theme with a child theme and a custom template file so in the parent's template.php file:

/**
 * Implements hook_theme().
 */
function mytheme_theme($existing, $type, $theme, $path) {

  global $theme; //need this to set $theme as the child theme

  $path = is_file( getcwd() .'/'. drupal_get_path('theme', $theme) . '/mytemplate.tpl.php' ) ? $path_to_child : $path;

  return array(
    'my_template' => array(
     'template' => 'mytemplate',
     'render element' => 'my_template',
     'path' => $path,
    ),
  );
}

It works but I feel this is clumsy. Also it doesn't find the new template if I put it in the templates folder. You can see that I've had to do an is_file() to look for the mytemplate.tpl.php file in the child theme.

I want my search order to be as follows:

  • themes/child_theme/templates/mytemplate.tpl.php
  • themes/child_theme/mytemplate.tpl.php
  • themes/parent_theme/templates/mytemplate.tpl.php
  • themes/parent_theme/mytemplate.tpl.php

This already works with page.tpl.php so clearly I'm missing something. Drupal can find page.tpl.php pretty much anywhere I put it. How do I make this work with my own custom template?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.