TomPich Posted 10 hours ago Share Posted 10 hours ago (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 10 hours ago by TomPich Correct typo Link to comment Share on other sites More sharing options...
da² Posted 10 hours ago Share Posted 10 hours ago 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 10 hours ago Author Share Posted 10 hours ago (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 10 hours ago by TomPich Found (not so satisfying) answer Link to comment Share on other sites More sharing options...
wbmnfktr Posted 7 hours ago Share Posted 7 hours ago 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...
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