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

Programmatically Force a Specific Style to be Loaded

bdami

New member
#1
Hi guys, brand new XF license holder here :) and just had a quick question about styles:

Is there a way (through hooks maybe?) to programmatically globally force a certain style to be used based on some arbitrary criteria, regardless of what the user has selected, or what the default forum style is? For example, say I want a certain style to always be loaded when I detect via PHP useragent parsing that the visitor is on a mobile device.

Thanks!
 

bdami

New member
#3
Thanks for the reply, Chris. I have seen that addon, however I was looking to just implement something myself, as I wanted to keep the additional overhead as minimal as possible, and don't need any Admin CP management of the rules or anything like that. Also I have some criteria which would not fit into that addon's rule system as it is tied to some of the website's existing structure.

I was mainly looking for tips on where I should start to get this functionality working, aka what hooks I need to be listening on, what objects/methods are used to set the style on each page load, ect, although I guess I could probably pick apart that addon's code to figure it out too. Just wanted to see if anyone could offer up some guidance before I do that though. :)
 

Chris D

XenForo developer
Staff member
#4
Yeah, I'm sorry.

I sometimes forget to see what forum threads are listed in as I normally find threads via What's New... I treated this as "I'm a user who wants this functionality" rather than "I'm looking to develop myself this functionality". Sorry about that :)

I will now bow out of the conversation as I have limited experience in this area, but definitely check out the code - specifically the code relating to user agents as if you're able to use some inspiration from that it could take you some way to achieve what you want (y)
 

bdami

New member
#6
Yeah, for the most part it does what I'm looking for, however it falls short it being able to handle some of the custom logic I need to employ, such as checking the values of specific browser cookies, or looking at records in a non-XF database table.

At the very least though it should serve as a good jumping off point if I'm able to pick it apart and successfully duplicate the same core functionality. Just was checking for pointers here first, before I did the extra legwork in case someone else had already done it. :)
 

bdami

New member
#8
I believe this is what you want:

http://xenforo.com/community/resources/different-style-per-domain-code-example.394/

It's a simple listener. You can change the code to use your own criteria.
Perfect, that's exactly what I was looking for, thank you! :)

From looking at the addon I see you are listening on the controller_pre_dispatch event, and getting a copy of the visitor instance which is what you are changing the 'style_id' property on. Just out of curiosity, how is XF picking up on that change to the style id, since from the looks of it its only happening within the scope of that listener function? It doesn't look like its being passed by reference or being returned or handed off to another function or anything like that.
 

Jake Bunce

XenForo moderator
Staff member
#9
Perfect, that's exactly what I was looking for, thank you! :)

From looking at the addon I see you are listening on the controller_pre_dispatch event, and getting a copy of the visitor instance which is what you are changing the 'style_id' property on. Just out of curiosity, how is XF picking up on that change to the style id, since from the looks of it its only happening within the scope of that listener function? It doesn't look like its being passed by reference or being returned or handed off to another function or anything like that.
The visitor object is a singleton, so all occurrences of the visitor object are the same instance. You are changing that value by class reference within the visitor instance, and that same instance is used elsewhere.