Jump to content
a-ok

Add styles to specific admin pages when certain user

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");
}

 

Share this post


Link to post
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...

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...