Jump to content
Soma

Config property to check for if on admin or frontend

Recommended Posts

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
}
  • Like 1

Share this post


Link to post
Share on other sites

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 */ }
  • Like 1

Share this post


Link to post
Share on other sites

+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)

Share this post


Link to post
Share on other sites

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") {
   ...
 }
}

Share this post


Link to post
Share on other sites

^^'

It's not about the user I think but context.

Share this post


Link to post
Share on other sites

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().

Share this post


Link to post
Share on other sites

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?

  • Like 1

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites
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

 

  • Like 1

Share this post


Link to post
Share on other sites

This should also work:

strpos($input->url, $this->wire('config')->urls->admin) === 0

so long the PW version is after this bug was fixed

  • Like 1

Share this post


Link to post
Share on other sites

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:

 image.png.17f2396c143520a943cd5fbb43b32ed3.pngimage.png.542cf5f100af172ef49b7e1856606f7a.png 

  • Like 5

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...