<?php
declare(strict_types=1);
namespace app\widgets;
use yii\base\Widget;
use yii\web\IdentityInterface;
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use app\widgets\forms\LanguageSelectorForm;
use app\helpers\LogHelper;
use app\helpers\RoleHelper;
/**
* User Language Selector
* How to use widget in views:
*
* <?= \app\widgets\LanguageSelector::widget() ?>
*/
class LanguageSelector extends Widget
{
private bool $_debugMode = false;
public ?LanguageSelectorForm $model = null;
// @return User $user object
public ?IdentityInterface $user = null;
public function init(): void
{
/**
* Call to parent constructor
*
* Always call the parent init method first to ensure any
* initialization logic defined in the base class is executed first.
*/
parent::init();
if ($this->model === null)
{
$this->model = new LanguageSelectorForm();
}
$this->model->language = $this->determineInitialLanguage();
}
public function run()
{
return $this->render('language-selector', [
'model' => $this->model,
]);
}
protected function determineInitialLanguage(): string
{
// Prefill model language if user logged in:
$user = $this->user ?? RoleHelper::userIdentity();
if ($user !== null && !empty($user->language))
{
return $user->language;
}
if ($this->_debugMode)
{
LogHelper::debug('LanguageSelector widget initial language', [
'language' => \Yii::$app->language,
'class' => __CLASS__,
'method' => __METHOD__,
'function' => __FUNCTION__,
'file' => __FILE__,
'line' => __LINE__
]);
}
return \Yii::$app->language;
}
}
Otherwise, call RoleHelper::userIdentity() to get the current user.
Why is this pattern used?
This pattern allows flexibility:
You can pass a user explicitly into the widget by setting the $user property before calling the widget,
User
but as you can see user var is not set, so even if i wanted to i couldnt pass it
Assistant
Actually, you can pass the user variable to the widget even if it’s not explicitly set in your code before calling the widget, because it is declared as a public property:
php
public ?IdentityInterface $user = null;
Why can you do that?
The widget() method accepts an array of initial property values which are assigned automatically.
In this case, when determineInitialLanguage() runs, it will use $this->user (the one you passed) rather than calling RoleHelper::userIdentity().
Why do this?
Makes the widget easier to test — you can inject a mock or test user.
Decouples the widget from global state — it doesn't always have to call a global user getter.
Supports more flexible use cases (e.g. admin changing the language for another user).
Summary:
Yes, you are supposed to set the public $user variable when you want to explicitly specify which user the widget should use.
The ?? operator provides a fallback to get the current user if no user was injected.
Leaving the $user property null and relying on RoleHelper::userIdentity() is perfectly fine as a default.
If you want to enforce it always being set from outside, you'd remove RoleHelper::userIdentity() fallback and make $user non-nullable. But that reduces flexibility.
Let me know if you want example usage or have other questions!
This works because Yii’s BaseObject (a parent class in the inheritance chain of Widget) supports mass assignment of properties via public setters or public properties from the config array passed to widget().
So:
You can pass user from outside becauseuser is a public property.
It does not need to be manually set within your widget code.
This feature is part of Yii’s base Object class constructor behavior, where all public properties can be set via the config array in construction time.
What if it were private or protected?
If the property was declared as private or protected, then mass assignment wouldn't work directly, and you’d have to have a setter method or some other way to pass the user.
Summary:
You can pass the user variable when using the widget, for instance:
php
<?= \app\widgets\LanguageSelector::widget([
'user' => Yii::$app->user->identity, // or some other object implementing IdentityInterface
]) ?>
That way, inside your widget’s code determineInitialLanguage(), the $this->user will use your passed user and not call RoleHelper::userIdentity().
If you want, I can show you a small example demonstrating this behavior. Would you like that?