1

I have a module witch generates a custom page:

function my_module_basket_menu() {
  $items["my-basket"] = array(
    "title"=>"My basket",
    "page callback" => "_my_basket_page",
    "access arguments" => array("access my_module_basket content"),
  );

  return $items;
}

function my_module_basket_theme($existing, $type, $theme, $path) {
  return array(
    'my_module_basket_page' => array(
      'template'  =>"my_module_basket_page",
      'render element' => 'page',
      'arguments' => array("listeProducts" => NULL, "key" => $key),
    ),
  );
}

function my_module_basket_theme_registry_alter(&$theme_registry) {
  $hooks = array('page');

  foreach ($hooks as $h) {
    // Add the module path on top in the array of paths
    array_unshift($theme_registry[$h]['theme paths'], drupal_get_path('module', 'my_module_basket'));
  }
}

function _my_basket_page() {
  // ...some treatment

  return theme("my_module_basket_page", $liste_produits, $key);
}

All works fine, but the problems start here: I want also to have a specific page.tpl, for example page_my_basket.tpl.php.

I tried to create a function my_module_basket_preprocess_page and change the theme there but the URL path I catch is the node of the page 404. Impossible to catch "my-basket". Any ideas? A different suggestion on how i can make it work?

EDIT

As suggested, i changed the name of my registry_alter function. From here when i look at the url (with a dsm()), i catch the good one, not the 404. So that part is ok.

So now i have to theme two page tpl, one for the page create in the module and a second one to replace the page.tpl.php The problem is this second page i add a function preprocess in my module :

function my_module_basket_preprocess_page(&$variables, $hook){

    $alias = drupal_get_path_alias($_GET['q']);

    if ($alias == "my-basket") {
      $variables['template_files'][]  = 'page_my_basket';      
    }
}

In my hook_theme function, i have that :

function oxo_wedze_basket_theme($existing, $type, $theme, $path){

  return array(
    'my_module_basket_page' => array(
      'template'  =>"my_module_basket_page",
      'arguments' => array("listeProducts" => NULL, "key" => $key)
    ),
    'page-my-basket' => array(
      'template'  =>"page_my_basket",
      'render element' => 'page'
    ),
  );
}

And that don't work, i still get the standard page.tpl.php

5
  • check if this link helps you drupal.stackexchange.com/questions/35280/…
    – arpitr
    Commented Oct 9, 2013 at 15:36
  • Your implementation of hook_theme_registry_alter() is not correct, it must start with your module name: my_module_basket_theme_registry_alter(). Please update your question after fixing this error and testing again.
    – Elin Y.
    Commented Oct 9, 2013 at 19:40
  • Елин Й, i change the name of the function and that works well, i do not have a 404 again. arpitr, i already see that question, but it doesn't works. I do as they say (i add the functions in my question) Commented Oct 10, 2013 at 13:04
  • In your hook_theme implementation check your return array. You are using dashes instead of underscores. page-my-basket should be corrected as page_my_basket.
    – Elin Y.
    Commented Oct 12, 2013 at 8:28
  • Have you had any luck solving your problem with the provided answers?
    – rooby
    Commented Oct 18, 2013 at 9:44

2 Answers 2

5
+25

Page templates have overrides already available based on path.

If you see template_preprocess_page() it has this code:

// Populate the page template suggestions.
if ($suggestions = theme_get_suggestions(arg(), 'page')) {
  $variables['theme_hook_suggestions'] = $suggestions;
}

and you can see that theme_get_suggestions() looks at the path and generates the template suggestions.

This means that if you have hook_menu() generate the path "my-basket" then you will be able to override its page template in your theme by naming it page--my-basket.tpl.php without having to do anything else, like use hook_theme() or manually add a template suggestion in the preprocessor, or alter the theme registry.

1

In order to have a different page.tpl.php for your page rendering, you can use the YOURMODULE_preprocess_page hook and add theme_hook_suggestion.

function my_module_basket_preprocess_page(&$variables) 
{
    $variables['theme_hook_suggestions'][] = 'page__my_basket';
}

The template file should be part of your theme and is located under the /templates folder using page--my-basket.tpl.php as filename. Don't forget to clear the Drupal cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.