5

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?

2 Answers 2

5

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);
1

I've used @Sharky's (thanks!) solution like:

use Joomla\CMS\User\UserFactoryInterface;

$user = Factory::getContainer()->get(UserFactoryInterface::class)->loadUserById($userid);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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