Search code examples
typo3repositorysettingsfindalldomain-model

TYPO3 Domain repository temporarily change settings


Normally if someone wants to run the function findAll(), he/she has to define the persistence Pid to let TYPO3 know where to look. If the persistence pid is not present, a function in the repository would do the trick. Like the following:

 public function initializeObject() {
    $querySettings = $this->objectManager->get(Typo3QuerySettings::class);
    $querySettings->setRespectStoragePage(false);
    $this->setDefaultQuerySettings($querySettings);
 }

But what happens if we do not have access to the repository? I mean the repository belongs to another extension and you can not edit it. The solution would be to extend the repository to an extend extension but this is sometimes too much to create an extended version only for that.

The question here is:

How do i temporarily, just for this request, change the settings of the repository, in this case change the setRespectStoragePage to false.


Solution

  • After some coding i finally figured it out.

    Step 1.

    create a protected variable where you wanna use the repository request:

    /**
    * @var \TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface
    */
    protected $querySettings;
    

    Step 2.

    Inject the QuerySettingsInterface in your current PHP class. If you want to use that in your controller, then you could do something like that:

    public function __construct()
    {
       parent::__contruct();
       $this->querySettings = $this->objectManager->get(QuerySettingsInterface::class);
    }
    

    otherwise

    public function __construct()
    {
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
        $this->querySettings = $objectManager->get(QuerySettingsInterface::class);
    }
    

    Step 3

    Finally you can set the settings like that:

    $this->querySettings->setRespectStoragePage(false);
    $this->contactRepository->setDefaultQuerySettings($this->querySettings);
    $contacts = $this->contactRepository->findAll();
    

    Simple and no extended extension needed just to implement the function in the repository.

    Best regards