1. 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

Discussion in 'XenForo Development Discussions' started by bdami, Sep 24, 2012.

  1. bdami

    bdami New Member

    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.

  2. Chris D

    Chris D XenForo Developer Staff Member

    Take a look at this add-on:

    Advanced Styling Rules

    This allows you to set criteria to load specific styles when certain criteria is met.

    One such criteria is viewing from a mobile device or specific user agent.
  3. bdami

    bdami New Member

    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. :)
  4. Chris D

    Chris D XenForo Developer Staff Member

    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)
  5. Digital Doctor

    Digital Doctor Well-Known Member

    I think that addon does exactly what you want.
    It would certainly be handy for you.
  6. bdami

    bdami New Member

    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. :)
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  8. bdami

    bdami New Member

    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.
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
  10. bdami

    bdami New Member

    Ah, that makes sense. Thanks again!

Share This Page