5

I'm trying to modify a theme so that when the term page or user profile page are displayed it doesn't generate the title at the top. I figured out how to do it on nodes but I just can't figure out what variable is used in the following code to identify a user or term page. Any thoughts?

Here's the code from my page.tpl.php in a Bootstrap v3 theme:

<?php if (!isset($node)): ?>
   <h1 class="page-header"><?php print $title; ?></h1>
<?php endif; ?>

The above example suppresses the title when the page being viewed is a node. I'd like to get this to work for user and term pages as well.

0

3 Answers 3

4

I think a better way to hide the title on user and taxonomy term pages would be to use a preprocess_page function in your themes template.php. This way you are keeping your page.tpl.php cleaner and separating logic.

/**
* Implements hook_preprocess_page
*/
function MY_THEME_preprocess_page(&$variables) {
    if( (arg(0)=='user' && arg(2)=='') || (arg(0)=='taxonomy' && arg(1)=='term' && arg(3)=='') ) {
        $variables['title'] = FALSE;
    }
}

This will only hide the title on the view pages, but it will still be there when editing content. This is done by checking that arg(2) is empty on user pages and arg(3) is empty on taxonomy term pages as if editing content these would contain the string "edit".

As pointed out by clive the line in your page.tpl.php would need to read:

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

Once you have added this code then you will need to clear the cache to see the changes.

3
  • 3
    Logically this is definitely the better approach for what the OP wants. Using menu_get_object() would arguably be preferable (I'm not arg()'s biggest fan either). There would be a slight change to the template here too, just to use if ($title) rather than if (!isset($node)). Otherwise you'll wind up with empty <h1>s
    – Clive
    Apr 4, 2014 at 14:40
  • Would using this method clear the title variable that's actually in place or set a new one to be tested against? See, I don't want to lose the actual title for breadcrumbs and meta data. I just want to suppress it's output at the top of the page.
    – Rob
    Apr 4, 2014 at 14:48
  • @Rob You'll only lose it in the context of the variables available in the page.tpl.php - it won't affect the rest of the page build (meta title is in html.tpl.php for example, different template with different vars). Breadcrumbs are already processed by that point too. Just check the rest of your page template file for more occurrences of $title...if you see none, you're golden
    – Clive
    Apr 4, 2014 at 14:49
3

There aren't any by default, but you can add them easily enough using hook_preprocess_page():

function MYTHEME_preprocess_page(&$vars) {
  if ( ($term = menu_get_object('taxonomy_term', 2)) ) {
    $vars['term'] = $term;
  }
  elseif ( ($account = menu_get_object('user', 1)) ) {
    // Best not to use $vars['user'], it'll only overwrite something.
    $vars['user_page_account'] = $account;
  }
}

And then in the template file:

if (isset($term)) {
  // term page
}
elseif (isset($user_page_account)) {
  // user profile
}

You might find false positives in there, as you'll get (for example) a term object on the taxonomy/term/[id]/edit page. Whether that affects you will depend on what theme you're using for what page. If it's a problem, you can use arg() to check what's in the URL, e.g.

if ( ($term = menu_get_object('taxonomy_term', 2)) && !arg(3)) {
  ...
4
  • I was trying to think of a way to not use arg() and this is it. If you use $variables['title'] = FALSE; in your preprocess_page function then there will be no need to edit the page.tpl.php at all...
    – Felix Eve
    Apr 4, 2014 at 14:30
  • 1
    I was about to mention menu_get_object() before I came across drupal.org/node/1468006, but trick was passing argument as '2', right?
    – arpitr
    Apr 4, 2014 at 14:38
  • 2
    @arpitr Yeah the 2nd arg corresponds to the position of the entity ID in the URL, when it's split into an array by the '/' character (zero-based)
    – Clive
    Apr 4, 2014 at 14:39
  • 2
    You can use menu_get_item() to get the actual router item and then figure out what to do from there. That can avoid multiple calls to menu_get_object() if you don't know what to use for $type. Though I think this gets weird when you have page manager overrides.
    – mpdonadio
    Apr 4, 2014 at 15:03
1

I tried both of the methods indicated and for various reasons they didn't quite work. Thus I opted for a css option. I added this to my css code for users and I'll add it for nodes and terms as well. Problem solved-ish. It's not elegant and it ends up making some of my pages looks a bit odd but it does work.

body.page-user h1.page-header {
  display: none;
}

Your Answer

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

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