2

I have many homepages and template file for every homepage and also a custom template for the front page the problem is when I change the default front page in admin/config/system/site-information to any other alias the page still have the page--front.tpl.php styles not the custom node style page--node--68.tpl.php , any help please ?

  • Have you tried to clear your theme cache? you need to clear the theme cache before they will be active. – iamroald Aug 14 '16 at 1:34
  • @iamroald : yes I cleared the cache, the problem is the url alias became the front page so it reads the front page template not it's own original template . – Eman Shaaban Aug 14 '16 at 2:35
4

In Drupal 7, template suggestions are added/modified in the preprocess function, in this case HOOK_preprocess_page().

Add this to the template.php file in your theme root directory; you may need to create this file.

The problem is that the page--node--id suggestion is weaker than page--front and does not get used. We need to move the page--node--id suggestion to the end of the theme_hook_suggestions array, so it overrides page--front.

Add this snippet. I have added comments to explain what each line of code does. Replace ThemeName with your theme name.

/**
 * Override or insert variables into the page template.
 */
function ThemeName_preprocess_page(&$variables) {

  // Check if this is the front page and a node is the front page.
  if ($variables['is_front'] == TRUE && isset($variables['node'])) {

    // Foreach over the theme_hook_suggestions array.
    foreach ($variables['theme_hook_suggestions'] as $key => $value) {

      // Does the suggestion match the current node?
      if ($value == 'page__node__' . $variables['node']->nid) {

        // Match, unset the template suggestion.
        unset($variables['theme_hook_suggestions'][$key]);

        // Push the template suggestion onto the end of the array, this will give
        // it precedence over page--front.
        $variables['theme_hook_suggestions'][] = 'page__node__' . $variables['node']->nid;
      }
    }
  }
}

I think this is the easiest/quickest way to move an array item to the end, where it will override page--front.

0

Hope this work on you this will allow you define/add your custom tpl base on url alias,

Example: you have url alias /sample then your custom tpl should be page--sample.tpl.php if you dump the variables variable in theme_preprocess_page you will notice that there's a new tpl suggested in theme_hook_suggestions 'page__sample' you jus need to create a new tpl in templates directory name it page--sample.tpl.php

/**
 * Implements template_preprocess_page().
 */
function exmaple_preprocess_page(&$variables) {
  if (module_exists('path')) {
    $url_alias = drupal_get_path_alias(str_replace('/edit', '', $_GET['q']));
    foreach(explode('/', $url_alias) as $path_part) {
      $template = 'page__' . $path_part;
      $variables['theme_hook_suggestions'][] = $template;
    }
  }
}

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.