Jump to content
Raymond Geerts

$user in /site/config.php in older version PW 2.2.17

Recommended Posts

I'm trying to check if the user is a superuser with the following IF statement.

if ($user->isSuperuser())

Normaly the works fine, even in the /site/config.php file.

The thing is, i'm working on an older site build with PW 2.2.17 which i can not upgrade at the moment.

Does anybody know how to get the current user object in to the config file?

I also tried $wire->user or wire('user') which seem all to be NULL

Share this post


Link to post
Share on other sites

Hi Raymond,

The problem is that the config file is parsed by ProcessWire before the API is ready, so the user object is not yet accessible.

I suppose you could create an autoload module and set/overwrite config values there, based on the user.

Cheers

  • Like 2

Share this post


Link to post
Share on other sites

Hi Wanze,

Thanks for the suggestion, will do it the way you describe and overrule the config values in the module.

Proost!

Share this post


Link to post
Share on other sites

Also look into using a /site/ready.php file, which will get called once the API is ready, but before any page rendering begins. The file receives all API variables, and you can put whatever you want to in it. There is also a /site/init.php file you can create, which does the same thing, but is called before the current $page has been determined.

Share this post


Link to post
Share on other sites

Also look into using a /site/ready.php file, which will get called once the API is ready, but before any page rendering begins. The file receives all API variables, and you can put whatever you want to in it. There is also a /site/init.php file you can create, which does the same thing, but is called before the current $page has been determined.

But not on such an old installation. 2.2.17 is definitely before 2.6.7.

  • Like 1

Share this post


Link to post
Share on other sites

I missed that. I read it as 2.6.17 for some reason. That 2.2.17 version should probably be upgraded.

Share this post


Link to post
Share on other sites

Both ready.php and init.php are not loaded in 2.2.17

Upgrading is not possible at the moment due to several reasons.

1st) the site is hosted on a dedicated server and upgrading PHP might jeopardise the working of the other sites on the server.

2nd) in version 2.2.17 multi language was not integrated in the core yet, altough the client wanted a multi language site, so its build in manualy. I'm worried that upgrading to a newer version of PW, the manualy build multi-language functionality might stop to work.

Thanks all for the suggestions and pointing in the right direction. I came up with a simple module that sets the template folder to /site/templates-dev/ for all superusers.

class DevelopmentTemplates extends WireData implements Module {

    /**
     * getModuleInfo is a module required by all modules to tell ProcessWire about them
     *
     * @return array
     *
     */
    public static function getModuleInfo() {

        return array(
            'title' => 'Development Templates',
            'version' => 001,
            'summary' => 'Overrules the config templates folder for development purpouses',
            'href' => 'http://www.processwire.com',
            'singular' => true,
            'autoload' => true,
            );
    }

    /**
     * Initialize the module
     *
     */
    public function init() {
        if (!$this->user->isSuperuser()) return;
        $this->addHookBefore('Page::render', $this, 'setDevTpl');
    }

    public function setDevTpl($event) {
            $page = $event->object;

            if ($page->template == 'admin') return;

            $config = wire('config');
            $config->urls->templates = 'site/templates-dev/';
            $config->paths->templates = $config->paths->root . 'site/templates-dev/';
            $config->debug = true;
    }
}

Note: only need this on older versions of PW, in my case 2.2.17. For newer PW versions use the following method as described on ProcessWire Recipes where you coudl use if($user->isSuperuser()) 

https://processwire-recipes.com/recipes/use-different-sets-of-template-files/

  • Like 3

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