Extension to Change Theme
Created 3 months ago by jzachary

I want to be able to preview a theme without it being live. There's situations where I find it faster to just have the theme developed on the live site for quicker preview of the live data or so others can see it for feedback. Overall probably not the wisest approach but sometimes its just quicker and less hassle.

Anyway, so I created a basic extension that will use the boot() function to change the theme being used on the front end. Sidenote, I'm still very green on Pyro and Laravel. Im confident in the theme layer but under the hood is a head scratcher, I'm slowly coming around though, i think sorting this would help some stuff click.

I can't figure out how to do a check if the use is logged in and an admin. I know this is available to use: if (auth()->user()->hasRole('admin') { }</code> but I cant sort out how you pull this in or reference it or what else needs to be added to make this work.

Here's the full Service provider:

<?php namespace Crt\PreviewthemeExtension;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
use Illuminate\Routing\Router;

class PreviewthemeExtensionServiceProvider extends AddonServiceProvider
{
    /**
     * Boot the addon.
     */
    public function boot()
    {
        // Run extra post-boot registration logic here.
        // Use method injection or commands to bring in services.
        // if (auth()->user()->hasRole('admin') {   }
        config(['streams::themes.standard' => 'crt.theme.confuikit']);
    }
}

I'm not quite sure if I need to bring something in with protected $commands = []; or if I need to have the correct class/thing/whateveritscalled added to the Use at the top.

I'm stumped. But, this is pretty dang cool that you can create an extension and do this sort of thing, It's taking a while but im slowly seeing just how powerful pyro can be...or a cluster if you have no idea what is going on. lol.

Appreciate any insight, education, mockery and help.

edster  —  3 months ago

That code you have should work, is it not working right now? You havn't given us any errors so its hard to say exactly what your issue is. Perhaps you want to check to see if they are logged in first before trying to reference the user? If this is the case put

<?php namespace Crt\PreviewthemeExtension;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
use Illuminate\Routing\Router;

class PreviewthemeExtensionServiceProvider extends AddonServiceProvider
{
    /**
     * Boot the addon.
     */
    public function boot()
    {
        if (auth()->check()) {
            if (auth()->user()->hasRole('admin')) {
                config(['streams::themes.standard' => 'crt.theme.confuikit']);
            }
        }
    }
ryanthompson  —  3 months ago

@jzachary nice one! This all looks good but to echo what @edster said, just make sure to validate that the user is logged in so you don't hit errors. Let us know how it pans out!

AS LONG as it's all done before this: https://github.com/anomalylabs/streams-platform/blob/1.7/src/StreamsServiceProvider.php#L285

jzachary  —  3 months ago

The if (auth()->check()) returns NULL, so trying to figure out why.

ryanthompson  —  3 months ago

So auth() is not built up yet in Laravel at boot.

I've run into thing's similar to this (disabling HTTP cache when logged in for example) and I got around it with using cookies. Once logged in - or if the user chooses "preview". Tag them with a cookie and check that during boot OR in the middleware and dispatch that same command with your theme configuration in place I guess worst case. LoadCurrentTheme can be fired whenever I think so.. that could help get around this race condition with the user not being loaded yet.

Does that make sense?

Choose preview from your addon somewhere, set the cookie / URI param / session variable / etc (something should be available) and check THAT in your boot method instead.

jzachary  —  3 months ago

Ah, that would seem pretty simple to set a session variable from the Extension settings. I'll try that approach next.