Jump to content

Any way to access $languages API var inside Page Class?


TomPich
 Share

Recommended Posts

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 by TomPich
Correct typo
Link to comment
Share on other sites

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 by TomPich
Found (not so satisfying) answer
Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...