TYPO3:通过Typoscript或ViewHelper渲染插件并更改设置

时间:2019-03-05 10:33:52

标签: typo3 typoscript extbase view-helpers

我想根据一些数据动态加载插件。首先,我尝试使用Typoscript进行此操作,但经过一些研究,我发现无法更改插件(see old forum entry)的设置。

我需要根据传递的数据更改settings.simplepoll.uid

这是我尝试过的文字:

lib.loadSimplepoll = USER
lib.loadSimplepoll {
    userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
    extensionName = Simplepoll
    pluginName = Polllisting
    vendorName = Pixelink

    switchableControllerActions {
      SimplePoll {
        1 = list
      }
    }

    settings < plugin.tx_simplepoll.settings

    settings {
        simplepoll {
            uid.current = 1
        }
    }
}

模板中的调用如下所示:

<f:cObject typoscriptObjectPath="lib.loadSimplepoll">{newsItem.simplepoll}</f:cObject>

弄清楚无法更改设置后,我尝试了一个viewhelper:

<?php
namespace Vendor\Extension\ViewHelpers;

use TYPO3\CMS\Core\Utility\GeneralUtility;  

class LoadSimplepollViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
    /**
     * @param int $uid Uid of poll
     * @return string
     */
    public function render($uid) {
        $cObj = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer');

        $configurationManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');

        $simplepollTs = $configurationManager->getConfiguration(
            \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
            'simplepoll',
            'Polllisting'
        );

        $ttContentConfig = array(
            'tables'       => 'tt_content',
            'source'       => 1030,
            'dontCheckPid' => 1
        );

        // returning this works perfectly!
        // but I need to change the "settings.simplepoll.uid"
        $data = $cObj->RECORDS($ttContentConfig);

        $cObj->start($data, 'tx_simplepoll_domain_model_simplepoll');
        $renderObjName = '<tt_content.list.20.simplepoll_polllisting';
        $renderObjConf = $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['simplepoll_polllisting.'];

        $renderObjConf['persistence']['storagePid'] = 394; // This does not work!
        $renderObjConf['settings'] = $simplepollTs;
        $renderObjConf['settings']['simplepoll']['uid'] = $uid;


        return $cObj->cObjGetSingle($renderObjName, $renderObjConf);
    }
}

viehelper的调用方式如下:

{vh:LoadSimplepoll(uid: '{newsItem.simplepoll}')}

现在,我可以使用此行更改民意调查的uid$renderObjConf['settings']['simplepoll']['uid'] = $uid;

我现在的问题是,它加载民意测验,但没有答案。我将此归结为一个事实,即该插件以某种方式不再知道Record Storage Page$renderObjConf['persistence']['storagePid'] = 394;行无济于事。

如何告诉插件Storage Pid

还是有另一种/更好的方式来加载具有更改数据的插件?

3 个答案:

答案 0 :(得分:0)

为什么不能在印刷稿中修改settings.simplepoll.uid
您只需要正确的构造即可对其进行修改。

对于单个值,您可以使用current,但请正确使用。这是一个stdWrap函数,需要对其进行评估。
如果默认情况下没有stdWrap评估,则可能适用于cObject类型的TEXT

settings.simplepoll.uid.cObject = TEXT
settings.simplepoll.uid.cObject.current = 1

或表示stdWrap,您需要按原样使用stdWrap

settings.simplepoll.uid.stdWrap.current = 1

数据传输的另一种形式是参数。只需建立一个关联数组作为数据参数并访问各个值即可:

流体:

<f:cObject typoscriptObjectPath="lib.arraytest" data="{a:'abc',b:'xyz'}" >
    inside text
</f:cObject>

和打字稿:

lib.arraytest = COA
lib.arraytest {
  10 = TEXT
  10.field = a
  10.wrap = /|/

  20 = TEXT
  20.field = b
  20.wrap = \|\
}

,其结果为/abc/\xyz\。请注意:f:cobject标记的内部文本将丢失,因为data参数的优先级大约为inner children

答案 1 :(得分:0)

为什么不能在印刷稿中修改settings.simplepoll.uid

因为扩展名 simplepoll 不能为其拼写设置处理任何stdWrap功能。

看一下代码:
使用此特殊设置here

$simplePoll = $this->simplePollRepository->findByUid($this->settings['simplepoll']['uid']);

没有stdWrap,只是普通用法。

将其与ext:news进行比较:

在使用任何设置之前,将对其进行处理。打字稿设置与插件中的设置的专用连接。如有必要,可以使用stdWrap:here

    $this->originalSettings = $originalSettings;
    // Use stdWrap for given defined settings
    if (isset($originalSettings['useStdWrap']) && !empty($originalSettings['useStdWrap'])) {
        $typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
        $typoScriptArray = $typoScriptService->convertPlainArrayToTypoScriptArray($originalSettings);
        $stdWrapProperties = GeneralUtility::trimExplode(',', $originalSettings['useStdWrap'], true);
        foreach ($stdWrapProperties as $key) {
            if (is_array($typoScriptArray[$key . '.'])) {
                $originalSettings[$key] = $this->configurationManager->getContentObject()->stdWrap(
                    $typoScriptArray[$key],
                    $typoScriptArray[$key . '.']
                );
            }
        }
    }

如您所见:
extbase不支持您使用错字stdWrap功能。
您(和每个扩展作者)都需要手工完成。甚至在extbase之前都是如此。

通过这种方式:由于您无法配置值,因此只能欺骗TYPO3(和插件):
如果您的uid数量很少,则每个uid都可以有一个变体

lib.loadSimplepoll123 < lib.loadSimplepoll
lib.loadSimplepoll123.settings.simplepoll.uid = 123

lib.loadSimplepoll234 < lib.loadSimplepoll
lib.loadSimplepoll234.settings.simplepoll.uid = 234

lib.loadSimplepoll345 < lib.loadSimplepoll
lib.loadSimplepoll345.settings.simplepoll.uid = 345

lib.loadSimplepoll456 < lib.loadSimplepoll
lib.loadSimplepoll456.settings.simplepoll.uid = 456

并像

那样称呼它
<f:cObject typoscriptObjectPath="lib.loadSimplepoll{newsItem.simplepoll}" />

或者您构建实现stdWrap功能的拉取请求,并将其发送给扩展作者。

答案 2 :(得分:0)

同时,我得到了Viewhelpter的帮助:

Viewhelper:

<?php
namespace Vendor\Extension\ViewHelpers;

use TYPO3\CMS\Core\Utility\GeneralUtility;  

class LoadSimplepollViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
{
    /**
     * @return void
     */
    public function initializeArguments()
    {
        parent::initializeArguments();
        $this->registerArgument('simplepollUid', 'int', 'Uid of simplepoll', false);
    }

    /**
     * @return string
     */
    public function render()
    {
        $configurationManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
        $simplepollTs = $configurationManager->getConfiguration(
            \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
            'simplepoll',
            'Polllisting');

        $cObj = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer');
        $renderObjName = '<tt_content.list.20.simplepoll_polllisting';
        $renderObjConf = $GLOBALS['TSFE']->tmpl->setup['tt_content.']['list.']['20.']['simplepoll_polllisting.'];

        $renderObjConf['settings'] = $simplepollTs;
        $renderObjConf['settings']['simplepoll']['uid'] = (int)$this->arguments['simplepollUid'];

        return $cObj->cObjGetSingle($renderObjName, $renderObjConf);
    }
}

在模板中调用viewhelper(不要忘记注册名称空间):

{vh:LoadSimplepoll(simplepollUid: '{newsItem.ipgSimplepoll}')}

就是这样。