Often used methods in Extbase repositories
In this post I show you some possibilities on how I do some changes on Extbase repositories when needed.
Some example should explain how to change the repository settings dynamically via Controller in an Extbase extension.
Default Query settings in Controller
With big repositories I often use a method to set repository settings dynamically, for example with the following method:
/**
* @param bool $respectStoragePage
* @param bool $includeDeleted
* @param bool $ignoreEnableFields
* @param bool $respectSysLanguage
* @param array $storagePageIds
* @return object|Typo3QuerySettings
*/
protected function getDefaultQuerySettings(
$respectStoragePage = false,
$includeDeleted = false,
$ignoreEnableFields = true,
$respectSysLanguage = false,
$storagePageIds = []
) {
/** @var $defaultQuerySettings Typo3QuerySettings */
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$defaultQuerySettings = $objectManager->get(Typo3QuerySettings::class);
$defaultQuerySettings->setRespectStoragePage($respectStoragePage);
$defaultQuerySettings->setIncludeDeleted($includeDeleted);
$defaultQuerySettings->setIgnoreEnableFields($ignoreEnableFields);
$defaultQuerySettings->setRespectSysLanguage($respectSysLanguage);
if (!empty($storagePageIds)) {
$defaultQuerySettings->setStoragePageIds($storagePageIds);
}
return $defaultQuerySettings;
}
Then it is possible to use it in the repository like this:
/**
* My special action
*
* @return void
* @throws
*/
public function mySpecialAction(): void
{
$storagePids = [123,234];
$defaultQuerySettings = $this->getDefaultQuerySettings(
true,
true,
true,
false,
$storagePids
);
$this->frontendUserRepository->setDefaultQuerySettings($defaultQuerySettings);
$user = $this->frontendUserRepository->findAll();
$this->view->assignMultiple(
[
'user' => $user,
]
);
}