replace template with other when conditions are met


You can alter the list of template suggestions in your preprocess hook:

function drop_preprocess(&$variables) {
  $variant = drop_get_variant_for_current_user($variables);
  if ($variant) {
    $suggestions = array();
    foreach ($variables['theme_hook_suggestions'] as $suggestion) {
      $suggestions[] = $suggestion
      $suggestions[] = $suggestion . '--' . $variant;
    $variables['theme_hook_suggestions'] = $suggestions;

This will add templates variants for all theme hook. So for instance, this allow you to override the node.tpl.php and page.tpl.php templates with node--special.tpl.php and page--special.tpl.php ones (when drop_get_variant_for_current_user() returns 'special'. If the template for a variant does not exists, it will simply be ignored.

Note that the order of suggestions is preserved. If you have three specialized node templates: node.tpl.php, node--special.tpl.php and node-article.tpl.php (the default one, the 'special' variant and the template for article nodes), when rendering an article node, the 'node-article.tpl.php' will be used since 'node_article' will be after both 'node' and 'node__special' in $variables['theme_hook_suggestions'].

Because it will run for all templates used on the page, the use a static cache in get_variant_for_current_user() so the actual variant won't be computed again and again for the same request/template.


  • The exact used template with be the last one in $variables['theme_hook_suggestions'] for which a template file exits. For instance if $variables['theme_hook_suggestions'] == array('node', 'node--special', 'node-article', 'node-article--special'), but only the node.tpl.php, node-article.tpl.php and node--special.tpl.php files exist, then node--special.tpl.php will be used.
  • The drop_get_variant_template() function is the one where you put your variant selection logic (and that has to be written).
Tags: Theming

Similar questions

Cancel if condition not met, show status
I have the Mimemail module set up to send HTML mails out, after a form is filled in an email is sent to the user based on a custom template. But the email should only be sent out if the user has previously filled in another form, if they haven't a message needs to be displayed and the email cancelled. I'm simply using the following code to check th...
Rewrite output for view field only when certain condition is met?
How to make a condition that if e.g. a checkbox is selected in the node saying something like e.g. 'Don't link this to the original content, redirect it somewhere else instead', then overwrite the default path of that node title to something custom? I found a module does it do what I'm trying to achi...
Querying for user's whose birthday is today plus other OR conditions
We have a date field attached to the user entity. It stores values for year, month and day, in the following format (looking at the field_date_of_birth_value column in the DB): 1987-06-08T00:00:00. We are using EFQ and wish to return users who's birthday is today OR some other conditions on another field also attached to the user entity (field_baby...
What's the difference between combining Rules conditions with AND and just have them follow one after the other?
Is there a difference between using AND in Rules Conditions and simply adding Rules Conditions one after the other?
Adding conditions to the template file of a view
I'm setting up a webshop in drupal and I bought a template. The tempate has a compare products function embedded, that works with a button on the product pages, however, I would only like that button to show up at certain types of products, like TVs and phones etc., but not for the rest. The compare button, however, is hardcoded into the template f...
How to render in template file with some conditions?
I want to use render function but I don't want to show all the data which is there in the particular field. So how can restrict something from rendering in template file. Scenario is: I have reference one field to another contentype, and I am rendering field in template file. But problem is even though the referenced node is not published, that dat...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.