是否可以通过 TYPO3 中的 Controller/Repository 更改常量?

Is it possible to change constants via Controller/Repository in TYPO3?

我想通过 initializeAction 在控制器中更改我的扩展中的常量。 它工作了一半。让我解释一下:

TYPO3 版本 8.7.7 使用 Extension Builder.

构建扩展

如果我在UserController.php

中使用以下代码
/**
 * initialize the controller
 *
 * @return void
 */
protected function initializeAction()
{
    ...
    $this->settings['groupDefaultId'] = (string)$newUserGroup[$key]->getUid();
    ...
}

然后我会调试它 php 我得到以下调试信息:

settings

array(12 items)

groupDefaultId => '53' (2 chars)

这是目前 PHP 中的正确值。但是,如果我在 Template > Constant Editor 下的后端检查该值,则该值不再存储。

我尝试了上面的代码和 setConfiguration API function

当我得到新 ID 时,我想在常量中设置哪个 ID,我使用以下命令启动函数:

文件:BaseController.php

(我的 UserController 扩展了 BaseController,用于从 Other-functions 拆分 CRUD-function)。

我给它一个字符串,因为在这个结尾 "way" 所有设置都作为字符串存储在 $this->settings 中。

// Set current Group Uid as defaultGroupId in Constants
$currentGroupIdHasSetInConstants = $this->setNewGroupIdInConstants((string)$newUserGroup[$key]->getUid());

我们跳转到 setNewGroupIdInConstants 函数:

文件: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);
}

首先这个函数跳转到addConstantsConfigurationForDefaultGroupId函数:

在文件中: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. \n for constants)
    $buildAdditionalConstant = '
plugin.tx_rmregistration.settings.defaultGroupId = '.$value;

    return $getConstants.$buildAdditionalConstant;
}

fetchConstants 函数:

在文件中:UserRepository.php

(通常它属于一个SysTemplatesRepository.php,但那些不存在。"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();
}

这是我的 updateConstants 函数的代码

在文件中: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;
}