簡體   English   中英

從插件模板中檢索內容元素字段?

[英]Retrieve content element field from within a plugin template?

我正在修改一個插件的模板,我想從內容元素中檢索一個字段。

使用f:debug我看到唯一可用的數據來自插件本身,沒有來自內容元素。

有什么辦法可以在插件設置中插入我需要的字段嗎?

例如。 就像是:

plugin.tx_plugin.settings {
    contentUid = TEXT
    contentUid.field = uid
}

我能想到的最好的方法是使用自定義 ViewHelper。 就像是:

namespace MyVendor\MyExtension\ViewHelpers;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

class ContentUidViewHelper extends AbstractViewHelper
{
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
    {
        $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
        return $configurationManager->getContentObject()->data['uid'];
    }
}

在您的流體模板中:

<mynamespace:contentUid />

這將獲取內容元素的 uid,但您可以通過這種方式獲取任何字段。 只需將data數組的鍵更改為您需要的字段即可。

在 controller 的相應方法(如listActionshowAction )中,您可以通過以下方式獲取內容元素的數據:

$contentObject = $this->configurationManager->getContentObject();
$this->view->assign('contentObjectData', $contentObject->data);

據我所知,您無法使用拼寫錯誤獲取該數據,但我從來不需要它,因為我一直在 controller 中使用上述代碼。

settings默認沒有stdWrap類型,只有string 所以你不能使用 cObjects 作為值。

對於一個(或幾個)設置,您可以自己在方法/類中進行stdWrap處理:

$settingsAsTypoScriptArray = $this->objectManager->get(TypoScriptService::class)->convertPlainArrayToTypoScriptArray($this->settings);
$contentObj = $this->configurationManager->getContentObject();
if ($contentObj === null) {
  $contentObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
// now override the values in the settings array with the processed value
$contentUid = (int)$contentObj->stdWrap($settingsAsTypoScriptArray['contentUid'], $settingsAsTypoScriptArray['contentUid.']);

如果你想有很多設置被stdWrap編輯,看看 EXT:news。 Georg 通過useStdWrap配置實現了一個有趣的概念。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM