When i got the new ID, which one i want to set in constants i start the function with the following command:
File: BaseController.php
(my UserController extends BaseController, for splitting CRUD-function from Other-functions).
I give a string to it, because at the end of this "way" all Settings are stored as string in $this->settings.
// Set current Group Uid as defaultGroupId in Constants
$currentGroupIdHasSetInConstants = $this->setNewGroupIdInConstants((string)$newUserGroup[$key]->getUid());
We jumpt to the setNewGroupIdInConstants function:
File: BaseController.php
/**
* Sets the Constant 'defaultGroupId' with new Value
*
* Incoming Requirement for this function: defaultGroupId = 0 this means
* defaultGroupId is not set in Constants
*
* @param string $newdefaultGroupId New Default Group ID for New Users
* @return bool TRUE defaultGroupId has changed | FALSE no changes done
*/
public function setNewGroupIdInConstants(string $newdefaultGroupId)
{
// Rebuild constants with new property and value
$newConstantsValue = $this->addConstantsConfigurationForDefaultGroupId($newdefaultGroupId);
// Add the new property with value to constants
return $this->userRepository->updateConstants($newConstantsValue);
}
First this function jumps to addConstantsConfigurationForDefaultGroupId function:
In File: BaseController.php
/**
* Build new constants value for defaultGroupId
*
* @param string $value The new value for defaultGroupId
* @return string The complete Constants value
* including the defaultGroupId Configuration
*/
public function addConstantsConfigurationForDefaultGroupId($value)
{
$getConstants = $this->userRepository->fetchConstants()['constants'];
// This Value has to look like this with the new line (for getting original code incl.
for constants)
$buildAdditionalConstant = '
plugin.tx_rmregistration.settings.defaultGroupId = '.$value;
return $getConstants.$buildAdditionalConstant;
}
fetchConstants function:
In File: UserRepository.php
(Usually it belongs to a SysTemplatesRepository.php, but those one doesn't exist. "Hello Core Team, we need a SysTemplatesRepository ;)".
/**
* Find Constants via sys_template Database Table
*
* @return array|NULL Result is array('constants' => queryResult) or NULL
*/
public function fetchConstants()
{ //
// Query Builder for Table: sys_template
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_template');
// Get Constants of Row, where RM Registration is included
$query = $queryBuilder
->select('constants')
->from('sys_template')
->where(
$queryBuilder->expr()->like(
'include_static_file',
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards('EXT:rmregistration/Configuration/TypoScript') . '%')
)
);
// Execute Query and Return the Query-Fetch
$query = $queryBuilder->execute();
return $query->fetch();
}
Here is my code of the updateConstants function
In File: UserRepository.php
/**
* Update Constants via sys_template Database Table ( Updates $this->settings )
*
* @param string $constants The new settings, that has to be stored in $this->settings
*/
public function updateConstants(string $constants)
{
// Query Builder for Table: sys_template
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_template');
$query = $queryBuilder
->update('sys_template')
->where(
$queryBuilder->expr()->like(
'include_static_file',
$queryBuilder->createNamedParameter('%' . $queryBuilder->escapeLikeWildcards('EXT:rmregistration/Configuration/TypoScript') . '%')
)
)
->set('constants', $constants);
$query = $queryBuilder->execute();
return ($query > 0) ? TRUE : FALSE;
}