Jump to content

Accessing $pages through API in another module (LoginRegister)


Krit65
 Share

Recommended Posts

Hey everyone,

I am trying to assign a page with template term-translation to variable $terms using the code below. This page is filled with fields that I use to translate certain texts.

<?php namespace ProcessWire;

	$terms = $pages->find('template=term-translation');

This snippet is at the start of code in a .module file (specifically LoginRegister). The problem is that the $pages is null, or atleast that is what the error is telling me.

 Error: Exception: Call to a member function find() on null.

I use this code in .php template files of my website and they work as they should.

Does anyone have a solution to this problem. If this topic was already in here somewhere, then I am sorry, but I could not find it.

 

Thanks for your help. 

Link to comment
Share on other sites

If your "$terms = ..." code is really the first line in a .module file and not within a class method, then I would expect, that it won't work. It would be executed when the .module php file is loaded and I guess at this very early moment the whole machinery is not yet ready. Have you tried to write this line in a module's init() or ready() method or in ready.php?

Link to comment
Share on other sites

6 hours ago, Krit65 said:

Does anyone have a solution to this problem. If this topic was already in here somewhere, then I am sorry, but I could not find it.

I'd suggest that you tell us what you try to achieve so that we can throw in ideas how that could be solved rather then asking how to fix your specific problem. The problem here is that we might be fixing a problem that does not even exist when we go another (better) route anyhow.

If you only want to have your translations available via the $terms variable in all your frontend template files, then just add your line of code in /site/templates/_init.php and it should work (assuming that you have $config->prependTemplateFile = '_init.php'; in your /site/config.php).

Link to comment
Share on other sites

8 hours ago, Krit65 said:

Because the URL of the page I am trying to translate

Why not using PW translation mechanism? LoginRegister module is ready to translate, you just have to enable languages support, create a new language, search the LoginRegister module in translations admin page and translate it. You may also find an already prepared language pack for your language (not sure it translates LoginRegister module but maybe, at least you'll get lot of PW translations already done).

https://processwire.com/docs/multi-language-support/

Link to comment
Share on other sites

I tried all of the suggestions. But none of them seem to work.

Thanks bernhard for the hint on how to help "myself". I will try to formulate what I am trying to do.

 

So I have a website, that has 4 language versions. Default is czech. Right now, I am trying to translate all the new labels and what-not to the 4 languages. I am using a page with template term-translations . In this page are fields(Multi-language) that contain different phrases which I just inject into HTML code. The problem is that I have to set a label of a button to submit and reset a registration form, but these buttons are added into the form in the LoginRegister module. That is what I am trying to do.

 

I am already using the multi-language system from PW, but now I hit a wall. (As you can see on the image below)  

image.thumb.png.ba5d13c471fc46b09bf3de362af1c786.png

 

Thank you for trying to help me. I am new to PW ?

Krit

  • Like 1
Link to comment
Share on other sites

Hi @Krit65

I don't use LoginRegister module but if you want to translate a string in your template file. You can try:

<?php echo __("Your String"); ?>

And then find the file in the backend translate tool (setup --> Languages --> Your Language --> Find file to translate --> find your template file)  to transfer the string to other language.

Gideon

  • Like 1
Link to comment
Share on other sites

@Krit65 Just in case, do you know the common way to translate modules in PW admin? Menu Setup > Languages > Your Language > Find files to translate > Select LoginRegister file > Translate what you need.

 

If it's needed to make translations editable from frontend, I would use PW translator to save phrases. I never used this API, I did a test but I don't know why it's breaking my existing translation file (it removes all previous translations and leaves 'file' and 'textDomain' empty):

$language = wire()->languages->getLanguage("english");
/** @var LanguageTranslator $translator */
$translator = wire(new LanguageTranslator($language));
$textDomain = '/site/templates/account-edition.php'; // Domain for LoginRegister should be '/site/modules/LoginRegister/LoginRegister.module'
$translator->loadTextdomain($textDomain);
$translator->setTranslation($textDomain, 'Logout', 'My translation');
$translator->saveTextdomain($textDomain); // Breaking my existing translation file

 

Another way, closer from what you're trying to do, is to hook one of the LoginRegister methods that build forms and change text values on the fly using your term-translation page, so in ready.php (code not tested):

wire()->addHookAfter('LoginRegister::buildLoginForm', function (HookEvent $event) {
  $terms = $pages->get('template=term-translation');
  /** @var InputfieldForm $form */
  $form = $event->arguments(0);
  $myField = $form->getChildByName('Email');
  $myField->set('label', $terms->myTranslationField);
});

Take care that there are several hookable methods that build a form in this module, ___buildLoginForm, ___buildRegisterForm... (check source code).

 

  • Like 2
Link to comment
Share on other sites

Quote

Just in case, do you know the common way to translate modules in PW admin? Menu Setup > Languages > Your Language > Find files to translate > Select LoginRegister file > Translate what you need.

This works for my needs. Did not know PW had this kind of translation function, was only using the multi-language fields.

Thank you everyone!

  • Like 1
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

×
×
  • Create New...