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

时间:2018-02-18 16:41:30

标签: php typo3 constants typoscript

我想通过 initializeAction 在Controller的Extension中更改常量。 它的工作方式是中途。让我解释一下:

TYPO3版本8.7.7 使用 Extension Builder构建扩展程序

如果我在 UserController.php

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

然后我在php中调试它后我得到了以下Debuginformation:

  

设置

     

数组(12项)

     

groupDefaultId => ' 53' (2个字符)

现在这是 PHP 中的正确值。但是,如果我在模板>下的后端检查该值常量编辑器该值不再存储。

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

1 个答案:

答案 0 :(得分:0)

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

文件: BaseController.php

(我的UserController扩展了BaseController,用于从其他函数中拆分CRUD函数)。

我给它一个字符串,因为在这个“方式”结束时,所有设置都存储为$ 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,我们需要一个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;
}