TomPich Posted January 21 Share Posted January 21 (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 January 21 by TomPich Correct typo Link to comment Share on other sites More sharing options...
da² Posted January 21 Share Posted January 21 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 January 21 Author Share Posted January 21 (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 January 21 by TomPich Found (not so satisfying) answer Link to comment Share on other sites More sharing options...
wbmnfktr Posted January 21 Share Posted January 21 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 January 22 Share Posted January 22 (edited) languages() global function is another way to get the instance, but wire()->languages property should work too. Edited January 22 by da² Link to comment Share on other sites More sharing options...
ukyo Posted January 22 Share Posted January 22 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() 1 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