Display user's rank on index with template variable

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
User avatar
teebling
Registered User
Posts: 113
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

Display user's rank on index with template variable

Post by teebling »

Hello,

Just a quick request - any way to use a template variable like {RANK_IMG} in overall_header.html?

Teebling
User avatar
Lumpy Burgertushie
Registered User
Posts: 69223
Joined: Mon May 02, 2005 3:11 am
Contact:

Re: Display user's rank on index with template variable

Post by Lumpy Burgertushie »

why? please explain exactly what you are trying to accomplish.

which user? where? etc.


robert
Premium phpBB 3.3 Styles by PlanetStyles.net

I am pleased to announce that I have completed the first item on my bucket list. I have the bucket.
User avatar
teebling
Registered User
Posts: 113
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

Re: Display user's rank on index with template variable

Post by teebling »

Lumpy Burgertushie wrote: Mon Mar 11, 2019 1:45 am why? please explain exactly what you are trying to accomplish.

which user? where? etc.


robert
Hi Robert,

There is a template variable in phpBB called {RANK_IMG}. When used in a template, it displays the current user’s rank image.

It is used in template files like viewtopic_body.html (viewing the posts of a topic) and memberlist_view.html (viewing profiles), among others.

It doesn’t however seem to work when used in the template file overall_header.html when browsing other types of pages.

For example, I would like the current user to see their rank whilst browsing any page on the forum - it would be great if there was a global template variable that would allow me to do this.

Does anyone know how this might be achieved?

Thanks,
Teeb
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Display user's rank on index with template variable

Post by mrgoldy »

This will require some core modifications, especially in the page_header() function within includes/functions.php.
Around this line add:

Code: Select all

$user_rank = phpbb_get_user_rank($user->data, ($user->data['user_id'] != ANONYMOUS ? $user->data['user_posts'] : false));
Then after this line you could add:

Code: Select all

'CURRENT_USER_RANK_TITLE'	=> $user_rank['title'],
'CURRENT_USER_RANK_IMG'		=> $user_rank['img'],
You should now be able to use {{ CURRENT_USER_RANK_TITLE }} and {{ CURRENT_USER_RANK_IMG }} to display the current user's rank where ever you want in the template.
Last edited by mrgoldy on Mon Mar 11, 2019 12:40 pm, edited 1 time in total.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
teebling
Registered User
Posts: 113
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

Re: Display user's rank on index with template variable

Post by teebling »

mrgoldy wrote: Mon Mar 11, 2019 12:30 pm This will require some core modifications, especially in the page_header() function within includes/functions.php.
Around this line add:

Code: Select all

$user_rank = phpbb_get_user_rank($user, ($user->data['user_id'] != ANONYMOUS ? $user->data['user_posts'] : false));
Then after this line you could add:

Code: Select all

'CURRENT_USER_RANK_TITLE'	=> $user_rank['title'],
'CURRENT_USER_RANK_IMG'		=> $user_rank['img'],
You should now be able to use {{ CURRENT_USER_RANK_TITLE }} and {{ CURRENT_USER_RANK_IMG }} to display the current user's rank where ever you want in the template.
Receiving this error after the above edits:

Code: Select all

Fatal error: Uncaught Error: Cannot use object of type phpbb\user as array in /Applications/MAMP/htdocs/includes/functions_display.php:1524 Stack trace: #0 /Applications/MAMP/htdocs/includes/functions.php(4345): phpbb_get_user_rank(Object(phpbb\user), '423') #1 /Applications/MAMP/htdocs/index.php(251): page_header('A WoW Classic D...', true) #2 {main} thrown in /Applications/MAMP/htdocs/includes/functions_display.php on line 1524
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Display user's rank on index with template variable

Post by mrgoldy »

Oh right, my bad.
Try this one:

Code: Select all

$user_rank = phpbb_get_user_rank($user->data, ($user->data['user_id'] != ANONYMOUS ? $user->data['user_posts'] : false));
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
teebling
Registered User
Posts: 113
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

Re: Display user's rank on index with template variable

Post by teebling »

Tried it, can't log in now as I receive:

Code: Select all

Fatal error: Uncaught Error: Call to undefined function phpbb_get_user_rank() in /Applications/MAMP/htdocs/includes/functions.php:4347 Stack trace: #0 /Applications/MAMP/htdocs/includes/functions.php(2485): page_header('Login') #1 /Applications/MAMP/htdocs/ucp.php(84): login_box('index.php') #2 {main} thrown in /Applications/MAMP/htdocs/includes/functions.php on line 4347
EDIT:

Okay so apart from the logging in bug above ^ this is correctly showing the user's rank, cool!

However I am also using you NASR extension Posey - any way I can get it to show special ranks as well? Cheers.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Display user's rank on index with template variable

Post by mrgoldy »

Ah no, that's not the issue.
phpbb_get_user_rank() is in functions_display.php, which I thought was included everywhere, unfortunately it is not.

So before the call to the phpbb_get_user_rank, add the following:

Code: Select all

if (!function_exists('phpbb_get_user_rank'))
{
	include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
}
But now you/we are treading on dangerous territory, as you're including a function file that is included in a lot of places, from a function that is literally called everywhere. The !function_exists() makes sure it's only included if it is not already. However, if the display functions are included elsewhere after the page header is called, without a check, there might still be double inclusions.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Display user's rank on index with template variable

Post by mrgoldy »

And about NASR, I did not finish that extension, Kasimi did.
I wouldn't honestly know anymore how to display them all everywhere.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
teebling
Registered User
Posts: 113
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

Re: Display user's rank on index with template variable

Post by teebling »

Understood.

So that has fixed the login issue, great.

Any way I can do this with your NASR extension {EXTRA_RANK_IMG} thing as well? That would be amazing.
User avatar
teebling
Registered User
Posts: 113
Joined: Sat Apr 14, 2018 7:38 pm
Contact:

Re: Display user's rank on index with template variable

Post by teebling »

mrgoldy wrote: Mon Mar 11, 2019 12:51 pm And about NASR, I did not finish that extension, Kasimi did.
I wouldn't honestly know anymore how to display them all everywhere.
Okay cool, thanks so much for your help so far. I have just PMed Kasimi asking for help with NASR stuff.
User avatar
Mick
Support Team Member
Support Team Member
Posts: 26505
Joined: Fri Aug 29, 2008 9:49 am

Re: Display user's rank on index with template variable

Post by Mick »

You should post in the extensions topic.
  • "The more connected we get the more alone we become" - Kyle Broflovski©
  • "The good news is hell is just the product of a morbid human imagination.
    The bad news is, whatever humans can imagine, they can usually create.
    " - Harmony Cobel
Post Reply

Return to “phpBB Custom Coding”