XF 2.2 What do we know before registered?

Robert9

Well-known member
When a user is registered, I can always use $visitor to know some things about him.
But what can I use, when a visitor has called register.php?

For example, there should be information about the language that we show him.
But from where can I fetch this information, please?
 
For example, there should be information about the language that we show him.
But from where can I fetch this information, please?
Also in XF::visitor() in PHP or $xf.visitor in templates. If the visitor is a guest, a guest user entity will be created.
 
Unfortunately, this seems not to work at all.

1. Open browser
2. open localhost with xenforo; we dont have $xf.visitor.language_id now.
3. Reload page, now we have $xf.visitor.language_id.
 
Currently I am here:

Code:
        $visitor = \XF::visitor();
        $language_id = $visitor->language_id;

        if ($language_id == 0)
        {
            $options = \XF::options();
            $language_id = $options->defaultLanguageId;
        }

First page: $visitor->language_id = 0;
after reload: $visitor->language_id = $options->defaultLanguageId;
after change to other language: $visitor->language_id =changed LanguageId


The next step is now to think about: set autoLanguage;

but finally, I don't like it. The best solution would be a pop-up with flags,
The best solution are two flags, one clickable, in the middle of the screen to choose if needed.
when browser.language != options->defaultLanguageId;

for this, i need something like: reload_page?lang=not_used_one (we have only two)
 
Last edited:
If the user's language ID is zero, it is assumed that the user is using the default language. You can use XF::language() and $xf.language to get an XF\Language instance for the current user.
 
Last edited:
No! It is assumed that we don't have any info about the user; and then the value is 0.

This makes no sense in this situation:

defaultLanguage=2
user first time on page; language = 0

then I would offer something with language 0/1/English.

The solution is above in post #6.
 
Fact first visit
1. User views page in language 2
2. click to register: $language_id = $visitor->language_id = 0;

Fact after reload:
1. User views page in language 2
2. while register $language_id = $visitor->language_id = 2;

Fact: with solution from above: user views register page in language_id=x, x>0 with language_id = x
 
After registration, the user's language will remain the same as it was before registration (see XF\Entity\User::_setupDefaults).
The visitor language (for a public application) is set to XF\Pub\App::start. In the case of a guest, this value could be the language_id cookie or 0 if there is no such cookie.
Also look at the XF\Language factory:
PHP:
        $container->factory('language', function($id, array $params, Container $c)
        {
            $id = intval($id);

            $cache = $c['language.cache'];
            if (!$id || !isset($cache[$id]))
            {
                $id = $c['options']->defaultLanguageId ?? null;
            }

            if (isset($cache[$id]))
            {
                $groupPath = File::getCodeCachePath() . '/phrase_groups';

                $class = $this->extendClass('XF\Language');
                return new $class($id, $cache[$id], $c['db'], $groupPath);
            }
            else
            {
                return $c['language.fallback'];
            }
        });
So 0 is the default language that is selected in the options.

So, as I said in post #7, you can use XF::language() and $xf.language to get an XF\Language instance for the visitor's current language. Even if XF::visitor()->language_id is zero, XF::language() will return an instance of the visitor's current language. You can get the ID using the getId function (XF::language()->getId()).
 
Thank you very much.

I dont know if we talk about the same things. I dont care now for "after registration"

Also, this sentence is not true, or maybe something different than i mean.

So 0 is the default language that is selected in the options.

My default language is 2 not 0.


My add-on will come in some days, then you can test it if you like.


The real life situation is like that:

default Language = 2
User comes to forum_home
User clicks [Register]
$visitor->language_id = 0
*checked with echo $visitor->language_id;

or reload page
User clicks [Register]
$visitor->language_id = 2
*checked with echo $visitor->language_id;

Because I need to know the language_id, i can't use it,
when it is 0, and i cant set it to 1, when it is 0; i have to set it to default (here 2)

While testing the situation, i found out to have these scenarios:

first visit
default Language = 2
page language = 2
$visitor->language_id = 0

second visit
default Language = 2
page language = 2
$visitor->language_id = 2

change language
default Language = 2
page Language = changed_id
$visitor->language_id = changed_id

Because of this, i have my solution from above.
if 0 => language_id = default_language


Is there anything wrong with my solution?
Why and in which scenario?
 
So, as I said in post #7, you can use XF::language() and $xf.language to get an XF\Language instance for the visitor's current language. Even if XF::visitor()->language_id is zero, XF::language() will return an instance of the visitor's current language. You can get the ID using the getId function (XF::language()->getId()).

Maybe you can specify the text above, please, possibly it is all about semantic:
I see:

1. defaultLanguage = language, the admin has chosen as default (i have 2)
2. language of the page a user views (pageLanguage)
3. the language a user has set for $visitor->language_id (userLanguage)

Normally pageLanguage = userLanguage, but not with the first call of a page.

XF::visitor()->language_id is zero, XF::language() will return an instance of the visitor's current language.

Can we say that "the visitor's language" is $visitor-language_id
and the pageLanguage (the language a visitor can see on the page) comes from XF:language()?



And again

first view of a page in my text scenario and click on [register]
defaultLanguage = 2
pageLanguage = 2
userLanguage = 0

reload and click on [register]
defaultLanguage = 2
pageLanguage = 2
userLanguage = 2

change language
defaultLanguage = 2
pageLanguage = chosen language
userLanguage = chosen language

The problem is the first page, when $visitor->language_id = 0
Then i have to check the pageLanguage (now i check the defaultLanguage)
As long, there is no difference between them, everything is ok.

But I will try to use the pageLanguage instead of defaultLanguage with
XF::language() and $xf.language
as suggested.
 
Last edited:
My default language is 2 not 0.
Yes. I wanted to say that 0 means the default language.
or reload page
User clicks [Register]
$visitor->language_id = 2
*checked with echo $visitor->language_id;
How did you get it? In my case, any number of page reloads returns 0. Do you choose the language manually?
but not with the first call of a page
Is the page displayed in a language other than the default? If the language is correct, then there is no problem.
Can we say that "the visitor's language" is $visitor-language_id
and the pageLanguage (the language a visitor can see on the page) comes from XF:language()?
Yes. The phrase function is in this class.
 
Here is what I do, step by step:

$defaultLanguage = 2

Open a browser, call localhost the first time.
(No cookie, no nothing there)

The page is shown in language 2!

Click on [register];
in my extension for pub/controller/register

i have

Code:
 $visitor = \XF::visitor();
        $language_id = $visitor->language_id;

        if ($language_id == 0)
        {
            $options = \XF::options();
            $language_id = $options->defaultLanguageId;
        }

when i echo $language_id would be 0, but because of the if, then, we have $language_id = 2



When I reload the page, the $language_id = $visitor->language_id; is 2

The problem was only the first call of the page.
 
I checked with

$visitor = \XF::visitor();
$language_id = $visitor->language_id;


this came later to solve it

if ($language_id == 0)
{
$options = \XF::options();
$language_id = $options->defaultLanguageId;
}
 
Top Bottom