Soma Posted August 1, 2012 Posted August 1, 2012 Would this makes sense or even easy possible to add a property, so you could simply check in moules/templates if you're in admin or frontend using: $config->isAdmin; Something like the $config->isAjax; If not, so those are the common checks that can be used to determine this (for reference): if( wire("page")->template == 'admin' ) { // is in admin } if( wire("page")->template != 'admin' ) { // is frontend } if( strpos( $_SERVER['REQUEST_URI'], wire('config')->urls->admin) !== false ) { // in admin } checking for only certain admin processes or areas is also possible // in a autoload module you could also use if( wire("process") == 'ProcessPageEdit' ) { // is editing page } if( strpos( $_SERVER['REQUEST_URI'], wire('config')->urls->admin . 'page/edit' ) !== false ) { // in editing page } 1
ryan Posted August 1, 2012 Posted August 1, 2012 That makes sense to me. To add to your list of ways to check, here's another that should be pretty bulletproof: if(strpos($page->url, wire('config')->urls->admin) === 0) { /* hello admin */ } 1
Nico Knoll Posted August 8, 2012 Posted August 8, 2012 +1 for $config->isAdmin; I needed it today and solved it like: if(strpos($_SERVER['REQUEST_URI'],wire('pages')->get(wire('config')->adminRootPageID)->url) !== false) 1
Adam Kiss Posted August 8, 2012 Posted August 8, 2012 (edited) ^^' Edited August 8, 2012 by adamkiss Nevermind, I am an idiot
Soma Posted August 8, 2012 Author Posted August 8, 2012 Nico, you could also use following to check if in backend. if($page->template == "admin") { .. } Or another way when using a hook function, for example function myhook(HookEvent $event) { $cur_page = $event->object; if($cur_page->template == "admin") { ... } }
Soma Posted August 8, 2012 Author Posted August 8, 2012 ^^' It's not about the user I think but context.
Soma Posted August 8, 2012 Author Posted August 8, 2012 Does it work in module's init() ? not really, in the init no page is loaded yet, but you could try to put it in loaded() or was it ready() function maybe or a hook from in the init().
adrian Posted November 24, 2015 Posted November 24, 2015 Just to add to this thread - I needed the check to run during init() and the best option I could think of was: if( wire("process") != 'ProcessPageView' ) { //in the admin } Can anyone think of any situation where this would fail? Perhaps some modules may run ProcessPageView in the admin? Maybe Nico's check for the admin path in the URL is a better option. Anyone have any more recent thoughts on this? 1
Sanyaissues Posted December 30, 2018 Posted December 30, 2018 Hi @adrian! I'm using your code to prevent some custom hooks to break things in the back-end. Did you find any case where the code fails? $wire->addHookBefore('InputfieldForm::render', function($event) { if( wire("process") != 'ProcessPageView' ) return; //Prevents classes from being added to the fields in the backend $form = $event->object; $form->addClass('uk-form'); });
adrian Posted December 30, 2018 Posted December 30, 2018 1 hour ago, Sanyaissues said: Did you find any case where the code fails? I don't think I found any problems with it, but I have also been using: strpos($_SERVER['REQUEST_URI'], $this->wire('config')->urls->admin) === 0 1
adrian Posted December 30, 2018 Posted December 30, 2018 This should also work: strpos($input->url, $this->wire('config')->urls->admin) === 0 so long the PW version is after this bug was fixed 1
tpr Posted January 1, 2019 Posted January 1, 2019 Spoiler alert: I'm building a new module that contains a collection of markup generators and other small helpers, and just added a new "isAdminPage" method. This accepts a Page or a Page ID and defaults to the input url. Here is a screenshot of the tests and the method itself: 5
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now