2

I am using the Forum module in Drupal 7. I want to change "Topic" with "foo", for example, in the General discussion forum page.

enter image description here

 

Where did I search for this

  1. I did check the template in the forum module file and for this page it is forum-topic-list.tpl.php. There I found that, it is printed by following code,

    <thead>
        <tr><?php print $header; ?></tr>
    </thead> 
    

    And in the description, the following statement has been provided.

    Available variables:

    • $header: The table header. This is pre-generated with click-sorting information. If you need to change this, see template_preprocess_forum_topic_list().

    Hence, there is nothing called "Topic" which I can just change as this header coming from the $header.

    Now, I did check template_preprocess_forum_topic_list() but it does not have provision to Change "Topic" string.

  2. Lastly, I did check the forum.module where I found the following function starting at line 914.

     function forum_get_topics($tid, $sortby, $forum_per_page) {
       global $user, $forum_topic_list_header;
    
       $forum_topic_list_header = array(
         NULL,
         array('data' => t('Topic'), 'field' => 'f.title'),
         array('data' => t('Replies'), 'field' => 'f.comment_count'),
         array('data' => t('Last reply'), 'field' => 'f.last_comment_timestamp'),
      );
    

    Now, in the above function you can see t('Topic'). I could simply replace it with t('foo'), but I think this is not the right way.

Any suggestion, how can I override this? Thanks.

2 Answers 2

1

If you check the code in template_preprocess_forum_topic_list(), you can see the $header is built with the global variable $forum_topic_list_header. It is possible to override it.

Most of the code here is copy from template_preprocess_forum_topic_list(). Only the part of code for building $header is needed.

/**
 * Implements hook_preprocess_forum_topic_list().
 */
function mymodule_preprocess_forum_topic_list(&$variables) {
  global $forum_topic_list_header;
  $forum_topic_list_header[1]['data'] = t('foo'); // Change "Topic" string

  // Create the tablesorting header.
  $ts = tablesort_init($forum_topic_list_header);
  $header = '';
  foreach ($forum_topic_list_header as $cell) {
    $cell = tablesort_header($cell, $forum_topic_list_header, $ts);
    $header .= _theme_table_cell($cell, TRUE);
  }
  $variables['header'] = $header;
}
8
  • Yes, this is changing the "Topic" to "foo". But I would like to know, how did you determine this part $forum_topic_list_header[1]['data'], as this part is not available in that hook, I mean from where did you arrive on this? Thanks
    – CodeNext
    Aug 13, 2016 at 7:51
  • @CodeNext What I did is to read code. It sounds stupid but it works :(
    – Jimmy Ko
    Aug 13, 2016 at 11:40
  • Okk, so we need to trace it back, and apply our code where we want to change the applications. So, this you knows, might be you would have played a lot with codes. But is there a way/module, So the non-coders can get a idea from which function it is originating, which path it is following and what hook we can apply? Or we learn it by playing with it only..:)
    – CodeNext
    Aug 13, 2016 at 11:54
  • 1
    Ahhhh...I understand it is hard for beginners. So that's why Drupal is hard :( in fact, my way is a little bit complicated because I would like to run in the same way as core does. Some others may just replace the string $variables['header'] = str_replace(t('Topic'), t('foo'), $variables['header']).
    – Jimmy Ko
    Aug 13, 2016 at 12:29
  • I have one question. Instead of $forum_topic_list_header[1]['data'] = t('foo'); if I take that part as it is form code, like, $forum_topic_list_header = array( NULL, array('data' => t('foo'), 'field' => 'f.title'), array('data' => t('Replies'), 'field' => 'f.comment_count'), array('data' => t('Last reply'), 'field' => 'f.last_comment_timestamp'), ); Will it be right thing to do?
    – CodeNext
    Aug 13, 2016 at 16:21
1

You could do it using the String Overrides module.

Provides a quick and easy way to replace any text on the site.

3
  • + 1, Yes, this is the option, No doubt. Even before posting question I did found this, and it is working well, but I am keeping this as a second option, just currently I am trying to do things hook-way. Again no doubt this is the right module for changing the t('string'). Thanks
    – CodeNext
    Aug 13, 2016 at 7:44
  • @CodeNext : your prior comment is perfectly ok/acceptable. But I think you should also somehow mention that in your actual question. Typically "programatically" is added to the title and/or question content to make that clear. Just a suggestion, not more then that ... Aug 15, 2016 at 9:28
  • @Pierre.Vriens, Thanks for pointing out. It was needed.
    – CodeNext
    Aug 15, 2016 at 9:44

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.