XF 2.0 Checking and changing the style on page load

CMTV

Well-known member
Hi!

I need to perform some checkings on every page of the forum and change its style (even for guests) depending on the results.

So I have this code:
PHP:
if(/* Condition */)
{
    // Set style 1
}
else
{
    // Set style 2
}

Where should I place this code? Maybe there is an event for this?
 
Ok, I figured it out. Though I am sure there is a more clean way to do so.

So I created a listener for visitor_setup event. Here is the code of event function:
PHP:
public static function switchTheme(User $visitor)
{
    // Checking stuff

    /** @var Option $options */
    $optionRep = \XF::repository('XF:Option');
    $optionRep->options()->offsetSet('defaultStyleId', '2');

    if($visitor->user_id)
    {
        $visitor->style_id = 2;
    }
}
 
It's possible to set a different style to be used depending on which node you're viewing.

That would be the recommended approach here, there's the code we use to do that in XF\ControllerPlugin\Node:
PHP:
if ($node->effective_style_id)
{
   $this->controller->setViewOption('style_id', $node->effective_style_id);
}
You might find a similar approach is better.

I'd recommend doing this with the controller_pre_dispatch or controller_post_dispatch event.
 
@Chris D I need to be able to set a style of the forum in general on every page of the forum (including admin cp and etc.). Basically I need an event that is always fired when any page is loading with an access to XF object ofcourse.

So it would be better to use controller_pre_dispatch or controller_post_dispatch for my situation?
 
Last edited:
Yeah I think so. I guess if the current approach you have works, then there might not be a particular reason to change it, but at least that's an alternative approach if your current approach is too restrictive.
 
Top Bottom