0

I'd like to create a module that use a set of custom templates depending on http status code

Very well inspired by Custom template for 404 error pages

Here is where i am so far

function my_module_preprocess_page(&$vars) {
  $header = drupal_get_http_header('status');
    if ($header == '404 Not Found') {
      //this
      $vars['template_files'][] = 'page__404';
      //or that 
      $vars['theme_hook_suggestions'][] = 'page__404';
    }
}

in my module templates folder , i have created a

page--404.tpl.php

this template is not overtaken !?

EDIT : i have also tried what's in there

function my_module_preprocess_page(&$vars) {
$header = drupal_get_http_header('status');
if ($header == '404 Not Found') {
  $vars['theme_hook_suggestions'][] = 'page__404';
}
}


function my_module_theme() {
  return array(
    'page__404' => array(
      'template' => 'page__404',
      'variables' => array('xxx' => null), 
      'path' => drupal_get_path('module', 'my_module')
    ),
  );
}
  • Possible duplicate of Override field template with custom module – sareed May 11 '17 at 13:49
  • 1
    Okay, in that case let's do some debugging. If you don't have Drush, get it :p. drush vset theme_debug 1 will add markup to the HTML so you can view tpl suggestions from your browser inspector. Make sure the ones you added are there. Also, make sure you don't have any dblog errors about your tpl not being found. – sareed May 12 '17 at 15:12
  • fantastic , page--404.tpl.php was suggested but the one taken in account was page__404 – Matoeil May 12 '17 at 15:25
  • 1
    To fix that and stay with standards, you can change 'template' => 'page__404' to 'template' => 'page--404'. The suggestions use underscore, but the tpl uses dashes. – sareed May 12 '17 at 16:11
0
function my_module_preprocess_page(&$vars) {
$header = drupal_get_http_header('status');
if ($header == '404 Not Found') {
  $vars['theme_hook_suggestions'][] = 'page__404';
}
}


function my_module_theme() {
  return array(
    'page__404' => array(
      'template' => 'page__404',
      'variables' => array('xxx' => null), 
      'path' => drupal_get_path('module', 'my_module')
    ),
  );
}

thanks to sareed, drush vset theme_debug 1 allowed me to see :

<!-- THEME DEBUG -->
<!-- CALL: theme('page') -->
<!-- FILE NAME SUGGESTIONS:
   * page--404.tpl.php
   * page--psnmssss.tpl.php
   * page.tpl.php
-->
<!-- BEGIN OUTPUT from 'my_module/page__404.tpl.php' -->

<!-- END OUTPUT from 'my_module/page__404.tpl.php' -->

so i changed page--404 to page__404 and it worked

how could i restrict the template suggestion only for the content region of the page ?

| improve this answer | |
  • 1
    Instead of replacing a page tpl, replace the region tpl. Use the same method, but different preprocess theme_preprocess_region instead of page. – sareed May 12 '17 at 16:14

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.