dongyue3795 2018-02-18 16:41
浏览 42
已采纳

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

I want to change a constant in my Extension in the Controller via initializeAction. It works the half way. Let me explain this:

TYPO3 Version 8.7.7 Extension build with Extension Builder.

If i use the following code in my UserController.php

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

and i'll debug it after that in php i got the following Debuginformation:

settings

array(12 items)

groupDefaultId => '53' (2 chars)

This is the correct value in PHP for now. But if i check that value in the Backend under Template > Constant Editor the value isn't stored any more.

I tried the code above and the setConfiguration API function.

  • 写回答

1条回答 默认 最新

  • duanmao2650 2018-02-18 16:54
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件