1

My setting here is like this:

I have a huge Druapl site here, and there's one single page that contains a views block displaying uploaded files with their taxonomy terms in a table. Title of this page is "Uploaded files".

Clicking on of these taxonomy terms calls the URL plus an additional query ?tid=123 applying a filter for that term on the table. So now only content of taxonomy tid 123 is displayed in the table and the URL ends with ?tid=123.

In that situation I want the title "Uploaded files" to be replaced by "Files of the taxonomy term 123", where 123 stands for the actual term connected to the tid 123. Let it be "To-Do-List".

Inside my theme's template folder I created a template file for that single page called page--node--nid.tpl.php, where nid stands for the actual node id of that page. I'm quite well able to manipulate the title of that page now when clicked on a term. At the place where the title is printed I can say <?php print $_GET['tid'] ?> and of course instead of "Uploaded files" it now prints the tid, here in my example: 123.

But how do I get the template to print the term instead of the id?

I know that there's a function called taxonomy_term_load to load the term connected with an id. But where would I call that function? And how do I pass maybe newly created $taxonomy_title into my page--node--nid.tp.php? Do I have to call some preprocess functions within the tags at the top of my page--node--nid.tpl.php?

To make matters even more difficult, there are terms from different vocabularies. In my example it sets the query to ?tid=123, but when I click on a term from another vocabulary it sets the query to ?tid_1=234

I came so far on my own, but do really very much appreciate a helping explanation on how and where to use that function...

2
  • 1
    Hi koivo, rather than editing your solution into the question please add it as an answer (answering your own questions is fine, encouraged even). It'll make life easier for others who come across this post with the same problem in future :)
    – Clive
    Jun 27, 2013 at 11:25
  • @Clive Thanks for the hint! Thought it might be unpolite to answer my own question... I now cutted the edit out, and pasted it into a new answer.
    – leymannx
    Jun 27, 2013 at 12:55

3 Answers 3

2

You can achievie that with contextual filters as well.

contextual filter: Content: Has taxonomy term ID

When the filter value is NOT available:

  • provide default value -> type: Taxonomy term ID from URL
  • Load default filter from node page, that's good for related taxonomy blocks
  • check the vocabulary which you want to use

When the filter value IS available or a default is provided:

Specify validation criteria -> PHP Code

$term = taxonomy_term_load($argument);
drupal_set_title($term->name);
return $argument;

It's done!

0
1

As I see, you want to change your node title, which is comes from page.tpl.php not node.tpl.php. So you need to make separate page.tpl.php for your content type.

You can make custom page.tpl for your content type by copying page.tpl.php and naming it like - page--CONTENT-TYPE.tpl.php

Then you need to workaround your template.php file. Follow nest steps.

You will have to get this term ID in template file at MYTHEME_preprocess_page() function at first. Do something like this --

function MYTHEME_preprocess_page(&$vars){
  if ($node = menu_get_object()){
        if($node->type == 'CONTENT_TYPE'){
            $vars['node'] = $node;

            //Get term ID or Term name, which ever is available. Yu can see the available values by doing dsm($node) here.
            //I don't have output of your $node object here, otherwise I can suggest exact.
            $term_id = $node->field_TERM_REFERENCE[LANGUAGE_NONE][0]['term_id']; 

            //If you have term ID then get term name.
            $term_name = taxonomy_term_load($term_id);

            //Prepare new title.
            $final_title = "Files of the taxonomy term ".$term_name;

            //Declare the new title variable to use at page.tpl.php.
             $vars['final_title'] = $final_title ;
        }
    }
}

Now go to your newly created page--CONTENT-TYPE.tpl.php & find $title output in that, it would be something like --

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

Replace it with --

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

You are done I guess. Clear your cache & see if it change or not. If it doesn't work then comment, but be prepare to give me output of your $node object by dsm($node).

4
  • 1
    You can avoid creating a new template file by adding some logic in your preprocess function that overrides the value of $title itself, but only on the specific page. Better yet, avoid writing custom code completely by using the Title Override module. Jun 26, 2013 at 20:15
  • @AdamBalsam - It's all upto a developer as I don't see any reason to stop understanding the system and do something on our own. As he needs title, so you have suggested, but you can't find solution for changing multiple label, which will contain dynamic info of some other fields. I have had several requirement related to title for particular content type & I have achieved doing like this..
    – RajeevK
    Jun 27, 2013 at 4:31
  • @RajeevK - Thank you very much for your detailed explanation. That helped me to better understand Drupal's logic. Finally I also followed Adam Balsam's advice and managed to do everything only in the template.php. See edit in my original question for code.
    – leymannx
    Jun 27, 2013 at 11:11
  • @koivo You are welcome. First thing is to understand the the logic behind the certain functionality which make system do something, then you can play with it and achieve what ever you want...if it's doable.
    – RajeevK
    Jun 27, 2013 at 11:30
0

Thanks to all who helped (also to Matt there -> http://mattwad.wordpress.com/2011/10/12/drupal-theming-change-a-page-title-in-template-php/).

I finally managed to do everything in the template.php's preprocess_page function (without any extra page-or-node.tpl.php), though I had to dpm first inside the process_page function to access all $vars.

function MYTHEMENAME_preprocess_page(&$vars) {
  if ($_GET['q'] == 'node/338' && isset($_GET['tid'])) {

      $tid_term = $_GET['tid'];
      $term = taxonomy_term_load($tid_term);
      $vars['title'] = $term->name;
    }
}

With $vars['title'] I finally set the actual page title to the tid's term after I took the tid from the URL with $_GET['tid']

3
  • Yeah, not too bad. Of course it depend on you to decide which method to use, but have you tried my solution?
    – Jack-PL
    Jun 27, 2013 at 18:35
  • @Jack-PL Yepp, tried that. But seems that doing that in the view itself is either too late or too early to set the page title. Anyway, you inspired me to something similar with your answer. Cause: I also have two view blocks on the same page containing a tag cloud. With your PHP suggestion I managed to set the CSS class of the tag links dynamically and by that to give the active tag link its own color. In the view i let one hidden PHP text field echo me some text, I than set as link class of the tag/term. <?php if ($_GET['tid']===$row->tid) { echo 'filter-active'; } ?>
    – leymannx
    Jun 28, 2013 at 10:55
  • 1
    hm.. I tested my answer and it works fine. In your case, I just think it would be better to keep all this dynamic block stuff in one place (Views). Anyway, I'm glad I inspire you to learn something new and to be helpful.
    – Jack-PL
    Jun 28, 2013 at 13:09

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.