• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Adding Additional Values/Params to $visitor in template

#1
How could i add values to the $visitor variable and get them out of sidebar_visitor_panel among other places?

Integration to our main site requires that we show some information about the user that is not in the xf database, or on the forum. Example: Number of comments on a custom made blog.

One of the places this value will need to be shown is in the template sidebar_visitor_panel

I have tried adding data using the listener container_public_paramas

Something to the affect of:

Code:
class CBlog_Stats {

  static function fbb_user(array &$params, XenForo_Dependencies_Abstract $dependencies){


   $params['visitor']['blog_stats'] = Array(
        "num_posts"=>5,
        "num_upvotes"=>10);
  }
 
}

but this hasn't worked.
 

Chris D

XenForo developer
Staff member
#2
You could probably do this with a template hook listener.

There's a hook you can use called "sidebar_visitor_panel_stats" which will allow you to append data to the stats section, where the message count etc. is.

So it would look something like this:

Code:
<?php
 
class YourAddOn_Listener
{
public static function templateHook($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)​
{​
if ($hookName == 'sidebar_visitor_panel_stats')​
{​
$hookParams['visitor']['blog_stats'] = array(​
"num_posts"=>5,​
"num_upvotes"=>10);​
);​
$contents .= $template->create('your_addon_template', $hookParams);​

}​
}​
That would then able you to use {$visitor.blog_stats.num_posts} and {$visitor.blog_stats.num_upvotes} in your template.
 
#3
Thats definitely reasonable for this case and will probably get me pretty far, but if there is away to add values to $visitor directly i would still like to know how.

Thanks for your help.
 

Chris D

XenForo developer
Staff member
#4
If there is, I don't know about it (quite plausible) but then again I've never needed it.

Generally, as $visitor is essentially a template parameter you'd want to do most template things using a template hook but there may be other ways to achieve that. I'm sure someone like Jake will have some more suggestions.
 

Jake Bunce

XenForo moderator
Staff member
#5
The question is why do you want to add values to $visitor? The only reason would be to make those values available everywhere $visitor is used. But for just one sidebar addition it doesn't warrant changing the $visitor object. Just retrieve the value and use it in that one place.
 

Chris D

XenForo developer
Staff member
#7
Everywhere is a hell of a lot of templates!

It still makes sense to add it to each hook it is required.
 
#8
right but seeing as there is container_public_paramas you can see how some things would help to be global. At the moment i am good, just i could think of reasons with external data where would be nice to be able to edit the visitor directly.