How can I fix the following deprecation error generated by my php code?

Factory::getDocument() is deprecated

$app = Factory::getApplication();
$wa = $app->getDocument()->getWebAssetManager();

if (!$wa->assetExists('script', 'keyselectmodal'))
    $document = Factory::getDocument();

2 Answers 2


Use Joomla\CMS\Application\WebApplication::getDocument(). In your code you're already doing this with $app->getDocument().

  • unexpected :: error. Commented Jul 9, 2021 at 10:40
  • In your code you only need to replace Factory::getDocument() with $app->getDocument().
    – Sharky
    Commented Jul 9, 2021 at 13:33

You can use this:

use Joomla\CMS\Factory;
$app = Factory::getApplication();
$document = $app->getDocument();

Or this:

use Joomla\CMS\Factory;
use Joomla\CMS\Document\FactoryInterface;
$container = Factory::getApplication()->getContainer();
$documentFactory = $container->get(FactoryInterface::class);
$document = $documentFactory->getDocument();

You may notice there's an "Undefined method" error in Intelephense despite it being defined and valid.

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.