Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need Drupal to use a custom page template if the URL contains a particular _GET parameter.

In D7 I would do something like this in the theme's template.php:

function mytheme_preprocess_page(&$variables) {
      if($_GET['isCustomTpl'] == true)
        $variables['theme_hook_suggestions'][] =  'page__myCustomTpl';

It doesn't work in D8 though (I would have been surprised by the opposite, considering all the architectural changes in the theme layer).

Any ideas?

Bonus question: is it possible to define such a function in a module, so that i can release it publicly without having the users to edit their themes?

share|improve this question
up vote 9 down vote accepted

You can implement the new hook_theme_suggestions_HOOK_alter() hook.

Alters named suggestions for a specific theme hook.

This hook allows any module or theme to provide altenative theme function or template name suggestions and reorder or remove suggestions provided by hook_theme_suggestions_HOOK() or by earlier invocations of this hook.

You can use it like so

function MYMODULE_theme_suggestions_page_alter(array &$suggestions, array $variables) {
  if (\Drupal::request()->query->get('foo') == 'bar') {
    $suggestions[] = 'page__myCustomTpl';

You can implement it in either a module or theme

share|improve this answer
Great, working perfectly! Hopefully it'll evolve into a fully-functional pjax module for d8! – Sifro Dec 22 '13 at 14:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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