TomPich Posted Tuesday at 04:48 PM Share Posted Tuesday at 04:48 PM (edited) Hello, I was wondering if there is a way to access the $language variable inside my DefaultPage class. I’d like, inside this class to define a lang property, rather then in the page template. public function __construct(Template $tpl = null) { parent::__construct($tpl); $this->lang = $languages->getLanguage; // doesn’t work, $languages is not accessible inside the class } Thanks Edited Tuesday at 04:49 PM by TomPich Correct typo Link to comment Share on other sites More sharing options...
da² Posted Tuesday at 05:03 PM Share Posted Tuesday at 05:03 PM Hello, You can use $this->languages, wire()->languages or wire('languages'). I prefer to use wire()->languages so PhpStorm knows what I'm using. Link to comment Share on other sites More sharing options...
TomPich Posted Tuesday at 05:13 PM Author Share Posted Tuesday at 05:13 PM (edited) Thanks, but unfortunately, I tried these before asking, because it seemed logical. All of this return NULL. But it seems it’s because I try to do this in the constructor. It’s apparently too early for that. When I do that in a method, it works. So maybe combined with a hook, I can acheive what I want. class HomePage extends DefaultPage { public function __construct(Template $tpl = null) { parent::__construct($tpl); $this->lang1 = $this->languages; // NULL $this->lang2 = wire()->languages; // NULL $this->lang3 = wire('languages'); // NULL } public function lang(){ return $this->languages; // NULL return wire()->languages->getLanguage()->name; // works! return wire('languages')->getLanguage()->name; // works! } } Edited Tuesday at 05:35 PM by TomPich Found (not so satisfying) answer Link to comment Share on other sites More sharing options...
wbmnfktr Posted Tuesday at 07:45 PM Share Posted Tuesday at 07:45 PM Shouldn't it be more like this: class HomePage extends DefaultPage { public function __construct(Template $tpl = null) { parent::__construct($tpl); $this->lang1 = $this->languages(); // added () $this->lang2 = wire()->languages(); // added () } public function lang(){ return $this->languages(); // added () } } Can't test right now but the docs say so. https://processwire.com/api/ref/functions/languages/ https://processwire.com/api/ref/page/get-languages/ Link to comment Share on other sites More sharing options...
da² Posted 16 hours ago Share Posted 16 hours ago (edited) languages() global function is another way to get the instance, but wire()->languages property should work too. Edited 16 hours ago by da² Link to comment Share on other sites More sharing options...
ukyo Posted 4 hours ago Share Posted 4 hours ago Before class initialized you can't access the languages. class HomePage extends DefaultPage { public function __construct(Template $tpl = null) { parent::__construct($tpl); // not initialized at here echo '<pre>' . print_r($this->wire()->modules->get('LanguageSupport'), true) . '</pre>'; } public function lang(){ // now you have some data on LanguageSupport module echo '<pre>' . print_r($this->wire()->modules->get('LanguageSupport'), true) . '</pre>'; // get default language echo '<pre>' . print_r($this->wire()->languages->getDefault(), true) . '</pre>'; // get available page languages echo '<pre>' . print_r($this->getLanguages(), true) . '</pre>'; } } its same for modules. you can access languages inside init(), ready() methods. not inside __construct() Link to comment Share on other sites More sharing options...
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