0

Here's a method that isn't working when I upgraded to TYPO3 10. The two issues I know of are...

ISSUE 1: $objectManager = new ObjectManager();

Should this be changed to... $objectManager = GeneralUtility::makeInstance(ObjectManager::class);?

The error the ISSUE 1 line produces is...

ArgumentCountError
Too few arguments to function TYPO3\CMS\Extbase\Object\ObjectManager::__construct(), 0 passed in /var/www/site/packages/something/Classes/Service/CanonicalService.php on line 63 and exactly 2 expected

ISSUE 2: $uriBuilder = $objectManager->get(UriBuilder::class);

PhpStorm reports says that method get is deprecated but I don't know what to do to fix this? I haven't really done DI stuff so anything you can do to help would be great!

THE CODE: Abbreviated version of packages/something/Classes/Service/CanonicalService.php

namespace Something\Somethingelse\Service;

// NOTE: TYPO3\CMS\Backend\Routing\UriBuilder is another class with same name but it appears it should only be used for backend
//   - https://stackoverflow.com/questions/27797031/how-to-create-a-page-url-from-page-uid
//   - https://docs.typo3.org/m/typo3/reference-coreapi/11.5/en-us/ApiOverview/Backend/EditLinks.html
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;

class CanonicalService
{
    /******* I removed all other methods BEFORE this to simplify *******/

    protected function rebuildCurrentUri()
    {
        static $uri;
        if ($uri !== null) {
            return $uri;
        }

        $objectManager = new ObjectManager();
        $uriBuilder = $objectManager->get(UriBuilder::class);
        if (!is_object(ObjectAccess::getProperty($uriBuilder, 'contentObject', true))) {
            return false;
        }

        $uri = $uriBuilder->reset()
            ->setAddQueryString(true)
            ->setAddQueryStringMethod('GET')
            ->setCreateAbsoluteUri(true)
            ->setArgumentsToBeExcludedFromQueryString(['cHash'])
            ->buildFrontendUri();

        return $uri;
    }

    /******* I removed all other methods AFTER this to simplify *******/    
}
god_is_love
  • 571
  • 5
  • 18

1 Answers1

1

ObjectManger is deprecated. It was used to instantiate classes that use arguments for the constructor. You can replace your code with the following:

  $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
  if (!is_object(ObjectAccess::getProperty($uriBuilder, 'contentObject', true))) {
     return false;
  }

GeneralUtility::makeInstance uses now Dependency Injection and since TYPO3 10 can instantiate repositories as well. You do not need Object Manager anymore and it is removed in TYPO3 12.

Aristeidis Karavas
  • 1,891
  • 10
  • 29