Jump to content

Recommended Posts

Posted (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 by TomPich
Correct typo
Posted

Hello,

You can use $this->languages, wire()->languages or wire('languages'). I prefer to use wire()->languages so PhpStorm knows what I'm using.

Posted (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 by TomPich
Found (not so satisfying) answer
Posted

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.

Posted (edited)

languages() global function is another way to get the instance, but wire()->languages property should work too.

Edited by da²
Posted

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()

  • Like 1
  • Thanks 1

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