Drupal theme layer gives us the flexibility to theme all the data used through out the site. We can also theme the page created from the drupal admin panel.
To Create a template for Basic Page Content type with URL alias follow the below steps.
Step 1: First create a page from drupal admin and give the URL alias. For this example i am creating a Page with “mytestpage” URL alias
Step 2: We need to implement hook_preprocess_page. Add the below code snippet in template.php file in your current theme folder (custom theme)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
function YOURTHEMENAME_preprocess_page(&$vars) { if (isset($vars['node']->type)) { // We don't want to apply this on taxonomy or view pages // Splice (2) is based on existing default suggestions. Change it if you need to. array_splice($vars['theme_hook_suggestions'], -1, 0, 'page__' . $vars['node']->type); // Get the url_alias and make each item part of an array $url_alias = drupal_get_path_alias($_GET['q']); $split_url = explode('/', $url_alias); // Add the full path template pages // Insert 2nd to last to allow page--node--[nid] to be last $cumulative_path = ''; foreach ($split_url as $path) { $cumulative_path .= '__' . $path; $path_name = 'page' . $cumulative_path; array_splice($vars['theme_hook_suggestions'], -1, 0, str_replace('-', '_', $path_name)); } // This does just the page name on its own & is considered more specific than the longest path // (because sometimes those get too long) // Also we don't want to do this if there were no paths on the URL // Again, add 2nd to last to preserve page--node--[nid] if we do add it in if (count($split_url) > 1) { $page_name = end($split_url); array_splice($vars['theme_hook_suggestions'], -1, 0, 'page__' . str_replace('-', '_', $page_name)); } } } |
Step 3: Now create a new file called page–mytestpage.tpl.php (format page–aliasurl.tpl.php) and drop it in your theme folder.
Step 4: Now clear the performance cache to refresh the theme registry.
Now the custom template will be picked up by drupal theme registry. To check if it is working i have given inline style with red color in page–mytestpage.tpl.php.
Compatability Drupal 7 Version