Jump to content

Add styles to specific admin pages when certain user


a-ok
 Share

Recommended Posts

 

I'm wanting to add in some styles (to "hacky" hide the buttons on a RepeaterMatrix field) on pages using a specific template. I've done this a lot with adding new rows to repeaters etc and the message below appears but I cannot seem to get it to add in the <style></style> using string replace. Any thoughts?

function eSubmissionStyles($event) {

    $page = $event->arguments(0);

    if ($page->template->name == 'events-detail') { // What's On detail

        $event->return = str_replace("</head>", "<style>html{background:red;}</style>\n</head>", $event->return);
        wire()->message('this message works above the above doesn't seem to do anything');

    }

}

if ($user->hasRole('e-submission')) { // Ony required for 'e-submissions'
    //$wire->addHookAfter("Pages::save", null, "eSubmissionStyles"); // I've tried this as well to no avail
    $wire->addHookAfter("Page::render", null, "eSubmissionStyles");
}

 

Link to comment
Share on other sites

Upon further testing it doesn't look like you can inject styles using the str_replace without using Page::render, but when using Page::render you cannot check $user or $page->template so I think I'm at a bit of a loss...

Link to comment
Share on other sites

You can use AdminOnSteroids to load a CSS. The module adds classes to the body, eg user-john, ProcessPageEdit-template-basic-page, role-superuser so you can add rules to target a specific page and user, eg body.user-john.ProcessPageEdit-template-basic-page p { color: red; }.

  • Like 3
Link to comment
Share on other sites

Hello,

to identify current page at admin side, there is problem using hooks inside "Page::render" process.
There is option to use "wire('input')" to get current page and it's properties (id, template...).

All that can be done inside simple module, but also inside site/ready.php (need to create ready.php).

Here is example of module (CustomScripts.module)

<?php namespace ProcessWire;

// CustomScripts.module

class CustomScripts extends WireData implements Module{
    
    public static function getModuleInfo(){
        return array(
            'title' => 'CustomScripts',
            'version' => 1,
            'summary' => 'Module to append custom CSS and/or JS files inside admin pages.',
            'singular' => true,
            'autoload' => true
        );
    }
    
    public function ready(){
        // check if admin side
        if ($this->wire('page')->template == 'admin') {
            // check if it is page (and not admin page list etc.)
            if (!is_null($this->wire('input')->id)) {
                // get page object
                $page = $this->wire('pages')->get($this->wire('input')->id);
                // check if it is a page with desired template
                if (!($page instanceof NullPage) && $page->template->name == 'basic-page') {
                    // check if current user has desired role
                    if ($this->wire('user')->hasRole('superuser')) {
                        // all checks are ok, now we can append desired stylesheet
                        $this->wire('config')->styles->add($this->wire('config')->urls->templates . "styles/my_custom.css");
                    }
                }
            }
        }
    }
} 

If you don't want to use module, take part from ready() method and place it inside ready.php (but without $this->).
Third option can be to do "fake invisible inputfield" and with that get more "power" (options)... "place field on desired templates, set desired CSS, JS files, set permissions, etc."

Regards.

EDIT: added one more check inside if statement !($page instanceof NullPage)

  • Like 4
Link to comment
Share on other sites

58 minutes ago, OLSA said:

Hello,

to identify current page at admin side, there is problem using hooks inside "Page::render" process.
There is option to use "wire('input')" to get current page and it's properties (id, template...).

All that can be done inside simple module, but also inside site/ready.php (need to create ready.php).

Here is example of module (CustomScripts.module)


<?php namespace ProcessWire;

// CustomScripts.module

class CustomScripts extends WireData implements Module{
    
    public static function getModuleInfo(){
        return array(
            'title' => 'CustomScripts',
            'version' => 1,
            'summary' => 'Module to append custom CSS and/or JS files inside admin pages.',
            'singular' => true,
            'autoload' => true
        );
    }
    
    public function ready(){
        // check if admin side
        if ($this->wire('page')->template == 'admin') {
            // check if it is page (and not admin page list etc.)
            if (!is_null($this->wire('input')->id)) {
                // get page object
                $page = $this->wire('pages')->get($this->wire('input')->id);
                // check if it is a page with desired template
                if ($page->template->name == 'basic-page') {
                    // check if current user has desired role
                    if ($this->wire('user')->hasRole('superuser')) {
                        // all checks are ok, now we can append desired stylesheet
                        $this->wire('config')->styles->add($this->wire('config')->urls->templates . "styles/my_custom.css");
                    }
                }
            }
        }
    }
} 

If you don't want to use module, take part from ready() method and place it inside ready.php (but without $this->).
Third option can be to do "fake invisible inputfield" and with that get more "power" (options)... "place field on desired templates, set desired CSS, JS files, set permissions, etc."

Regards.

This looks like just the ticket and good to learn about this. Much appreciated. I'll give it a whirl and let you know how I get on.

Link to comment
Share on other sites

4 hours ago, tpr said:

You can use AdminOnSteroids to load a CSS. The module adds classes to the body, eg user-john, ProcessPageEdit-template-basic-page, role-superuser so you can add rules to target a specific page and user, eg body.user-john.ProcessPageEdit-template-basic-page p { color: red; }.

Aha! This looks gooood. Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...