XF 2.2 How to assign a forum style to logged in users?

Anatoliy

Well-known member
I'd like to make a default style "guest oriented" and another one - normal (for members). I'd remove from the default style code that is not used by guests from html and less templates (I guess everything related to liking, voting, posting etc can be safely removed).

Can something be extended somewhere so it would assign a specific style to logged in users?
 
I would do the opposite: Create a style for guests and assign it to them and make it not selectable for members ;)

As said before, this should be doable with an event listener. One of my tries was for event dispatcher_pre_render:

PHP:
    public static function dispatcherPreRender(
        \XF\Mvc\Dispatcher $dispatcher,
        \XF\Mvc\Reply\AbstractReply $reply,
        string $responseType
    ) {
        $reply->setPageParam('style_id', 2);
    }
For testing purpose I tired to assign this style to all users - no success.

Another try with event templater_setup only caused problems: I tired to get the desired style entity (namespace XF\Entity\Style) and then did this: $templater->setStyle($setStyle). Result was a PHP error (because the expected object was an instance of XF\Style). Tried to cast the object, but that was no solution and brought new errors. :(
 
This is working with the dispatcher_pre_render event, but using the setViewOption function:
PHP:
    public static function dispatcherPreRender(\XF\Mvc\Dispatcher $dispatcher, \XF\Mvc\Reply\AbstractReply $reply, $responseType)
    {
        $reply->setViewOption('style_id', XF::visitor()->user_id ? 2 : 1);
    }
2 is the user style id and 1 is the guest style id.
 
I didn't understand a word, but sounds very promising. )
I guess I should go to read docs about an event listener.

Thanks, NikitOS!
 
Last edited:
That's the best way to learn how it's built - to take a working one and to brake it apart. )

That's The Way
Aha aha
I like it
Aha aha

Thank you!!!
 
I was busy previous days, but today I installed the addon on my local server and it works!
Thank you NikitOS!!!
 
I created a small addon that prevents guest and users from changing the style and sets the styles specified in the options. This does not apply to administrators.
Options: /admin.php?options/groups/mv_cForceUserStyle/.
This is awesome, thank you!
Is there any way to make it so users can still choose a different style if they want?
So say this sets the default, but the user can still go into the style chooser and override it?

I tried commenting out XF/Entity/User.php and that allows the style choose to come up, but any selection there is ignored.
 
@FidoMike, this add-on forces the user to use the style selected in the options. Do you just want to change the selected style once for all users?
 
@FidoMike, this add-on forces the user to use the style selected in the options. Do you just want to change the selected style once for all users?
Sorry I should explain I modified it to set the style based on current domain, that works great, but I've lost the ability for users to choose their own style and styles set on a node don't work either. I was hoping for either a way to detect the style set on a node or a user's preference before setting the style, but I wasn't able to find a way to do that. Or maybe a way to set the default style on load instead of forcing it. I can post the contents of listener.php if that helps.
 
Thanks but I already have that set, I'm trying to set it or change it in the listener depending on certain conditions. Basically a less forceful version of what you made. Is there a way to check if the user has chosen a style before forcing one on them?

Found it, the user's style choice is held in XF::visitor() as style_id, therefore I can check that before I force a new style onto them.
For anyone interested in this, here is a bit of sample code:

PHP:
$visitor = \XF::visitor();
echo $visitor['style_id'];
 
Last edited:
I also want you to redirect to a different style when you register. But members could change styles afterwards. That way, they can continue to change style, as can I.
 
Last edited:
I created a small addon that prevents guest and users from changing the style and sets the styles specified in the options. This does not apply to administrators.
Options: /admin.php?options/groups/mv_cForceUserStyle/.

The accessory is great! But couldn’t it be resolved to continue to change style?
 
@styui, I think this add-on solves the problems that @Anatoliy had.
Regarding your question: isn't this behavior out of the box? Guests see the default style and it will also be selected from them during registration. An exception is a style change by a user while he was a guest.
 
I understand. The administrator can still change the style. I thought users would somehow be able to do that. That's what I want.


The accessory is great, just don’t hinder a later change of style.

Could this be solved?

What do I need to change?

Thanks in advance for the answer!
 
If you want to allow style change for all registered users, you can replace MV\CForceUserStyle\XF\Entity::canChangeStyle with this:
PHP:
    public function canChangeStyle(&$error = null)
    {
        return $this->user_id && parent::canChangeStyle($error);
    }
 
thank you very much for your help! It works! You only need to rewrite it in Listener.php according to the instructions above.

Thank you very much again, dear NikitOS!
 
Back
Top Bottom