11

I am trying to hide the title of the current page (as printed by page.tpl.php) if the current page is 1) a node in full display (as opposed to teaser mode), AND 2) the node type is either concert or bio. I print the node title for these content types directly in the node content, so I want to hide the duplicate being printed by page.tpl.php. I am not able to get this to work directly in page.tpl.php, and my reading suggests the best practice is to handle template logic in template.php using preprocess and process functions. So, I am trying to hide the current page title (with the above 2 conditions) using template_preprocess_page(). This is my code.

<?php
    function MYTHEME_preprocess_page(&$variables) {
        if (!empty($variables['node']) && $variables['node']->type == 'concert') {
            hide($variables['title']);
        }
    }
?>

I read How do I test for node type to print a variable from template.php? and the documentation page for template_preprocess_page(), and various other sites, but I just can't seem to get this to work. I saw suggestions that you have to call isset() on $variables['node'] before testing for the node type, but that didn't work either. I didn't see a clear solution posted anywhere, and I'm at my wit's end.

7 Answers 7

9

and my reading suggests the best practice is to handle template logic in template.php using preprocess and process functions.

Decoupling the logic from the rendering code simply means the code in template.php sets a variable that is checked in the template file. In this way, the logic is all contained in the template.php file, while the rendering code is all in the template files.

template.php

function mytheme_preprocess_page(&$variables) {
  if (!empty($variables['node']) && $variables['node']->type == 'concert') {
    $variables['show_title'] = FALSE;
  }
}

page.tpl.php

<?php if ($show_title && $title): ?><h1 class="title" id="page-title"><?php print $title; ?></h1><?php endif; ?>

hide() doesn't work because $variables['title'] is a string, not a rendering array. hide() is always used with a rendering array, and it doesn't have any effect with a string, not even when the string is passed to render().
Actually, passing a string (or any value that is not a reference to a variable containing an array) to hide() would cause a PHP error, since the function uses this code.

function hide(&$element) {
  $element['#printed'] = TRUE;
  return $element;
}

Unsetting $variables['title'] with unset($variables['title']) doesn't work too, as template_process_page() would get the page title from drupal_get_title().

if (!isset($variables['title'])) {
  $variables['title'] = drupal_get_title();
}
3
  • Of course you could also change the actual $title variable with mytheme_process_page(&$variables) { $title = false; }.
    – RobW
    Commented Nov 28, 2012 at 4:39
  • 4
    @RobW that would be mytheme_process_page(&$variables) { $variables['title'] = false; } and that works BTW.
    – Duncanmoo
    Commented Sep 30, 2013 at 9:35
  • Another note on this; as long as your theme wraps the <?php print $title; ?> in <?php if (!$title_hidden): ?> you can simply set $variables['title_hidden'] = TRUE;
    – Duncanmoo
    Commented Nov 13, 2013 at 18:30
2

You can manage this using drupal_set_title().

function mytheme_preprocess_page(&$vars){
    if (isset($vars['node']) && $vars['node']->type == 'news'){
        drupal_set_title('');
    }
}
2
  • This didn't work for me. Could something else be overriding template_preprocess_page()? I've used Theme Developer module to inspect the duplicate page title and confirmed that it is being called from page.tpl.php. Should I think about deleting the title from page.tpl.php and then ADD it back where I want it using template_preprocess_page()? I want the title to render, for example, when the page contains a view of nodes, but not when the page is a single node of a certain type/types. Commented Sep 16, 2012 at 14:34
  • 1
    This also could strip away important information from visitors using screen readers. It also strips off the title of the page in the users tab. Think accessibility from the start.
    – Rick
    Commented Nov 13, 2012 at 15:56
1

Try this assuming your if condition is correct :)

 function MYTHEME_preprocess_page(&$variables) {
        if (!empty($variables['node']) && $variables['node']->type == 'concert') {
            unset($variables['title']);
        }
    }
1
  • It doesn't work: template_process_page() would set $variables['title'] with the value returned from drupal_get_title().
    – avpaderno
    Commented Nov 13, 2012 at 17:38
0

Less complicated way is by printing this in your page.tpl.php

<?php hide($title); ?>

But if you want it to be dynamic I would suggest to use the preprocess_page as suggested by others.

1
  • 1
    It doesn't work. hide() is used with rendering arrays, not strings.
    – avpaderno
    Commented Nov 13, 2012 at 17:33
0

In CSS, I did this to my theme

/*    Front Page - hide title  */
body.front header h1 {
 position: absolute; 
 left: -999em;    
}

As recommended by http://www.alistapart.com/articles/now-you-see-me/

Why? Because you still want to have the header and h1 'visible' to screen readers for accessibility. If you simply strip off the value of the H1, you're robbing your blind and low vision visitors of some potentially important information.

2
  • This will not work. Why? Because you can't add conditional CSS without help from programming site n
    – AKS
    Commented Nov 13, 2012 at 16:28
  • Drupal adds classes to elements such as 'front' used above. For myself, I need to hide the title on the front page but leave it 'visible' to screen readers. Therefore, I can use the logic of cascading rules to clearly define when I need it hidden from sighted users and when I do not.
    – Rick
    Commented Nov 13, 2012 at 16:42
0

the suggestion of mytheme_preprocess_page works for me but if is used $vars['title_hidden'] = TRUE;

function mytheme_preprocess_page(&$vars) {
  if (isset($vars['node']->type) && $vars['node']->type == 'type') {
    $vars['title_hidden'] = TRUE;
  }
}
0

The correct function is

function mytheme_preprocess_node(&$vars) {
}

instead of

function mytheme_preprocess_page(&$vars) {
}

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.