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

报告相同问题?

悬赏问题

  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序
  • ¥20 Win11测试yolov4,“找不到nvcuda.dll”怎么办?
  • ¥15 simulink绘制bode图
  • ¥15 php_network_getaddresses: getaddrinfo failed: Name or service not known
  • ¥15 用msg发消息出现的问题
  • ¥15 unity3d机械臂
  • ¥20 工控机出现散热器转一下停一下这种情况,是什么原因造成的