2

I'm looking for an extension for Joomla! 4.0 to replace a deprecated method which was used to load an user by userid. The method used actually is:

$user = Factory::getUser($userid);

The issue is that in the error log it is recommended to use Factory::getApplication()->getIdentity() instead, but this method doesn't accept params! The other method that I found is Factory::getApplication()->loadIdentity(), but this one accepts User object as its parameter.

So which is the non-deprecated method to load a user object by passing userid?

1

Use Joomla\CMS\User\UserFactory. You can get it from the service container:

use Joomla\CMS\User\UserFactoryInterface;

$userFactory = $container->get(UserFactoryInterface::class);

Or using alias:

$userFactory = $container->get('user.factory');

Where $container is the container. If you're doing this inside service provider file (services/provider.php), it's already defined. Otherwise, if using container as service locator inside your classes, get it from main factory:

$container = \Joomla\CMS\Factory::getContainer();

To create a user instance:

$user = $userFactory->loadUserById($id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.