[TYPO3-mvc] Re: Create predefined fe_groups when activating extension
Genser
typo3 at genser.eu
Mon Dec 7 11:44:11 CET 2015
I recently had a similar problem and wrote this function:
/**
* @param string $frontendUserGroupTitle
* @return \TYPO3\CMS\Extbase\Domain\Model\FrontendUserGroup
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
*/
private function getFrontendUserGroupByName(string $frontendUserGroupTitle) {
if (0 == $this->frontendUserGroupRepository->countByTitle( $frontendUserGroupTitle )) {
$fe_group = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
'\\TYPO3\\CMS\\Extbase\\Domain\\Model\\FrontendUserGroup', $frontendUserGroupTitle
);
$fe_group->setPid( $this->settings['groups_PID'] ); // pageID where the group should be created
$this->frontendUserGroupRepository->add( $fe_group );
return $fe_group;
} else {
$fe_group = $this->frontendUserGroupRepository->findByTitle( $frontendUserGroupTitle );
return $fe_group[0];
}
}
It returns a frontendUserGroup (and creates it if it does not already exists) and you can use it like this, in your controller:
$feUserObj->addUsergroup( $this->getFrontendUserGroupByName( $feGroupTitle ) );
followed by:
$this->frontendUserRepository->update( $feUserObj );
$this->persistenceManager->persistAll();
Obviously you need to inject $frontendUserGroupRepository and $persistenceManager but it works for me. That way you don't need to insert data with a fixed UID (bad idea in my opinion).
Hope this helps.
More information about the TYPO3-project-typo3v4mvc
mailing list