1

我在 RenderType = selectMultipleSideBySide 中使用 T3 后端的 TCA 类型选择

这里是 TCA 代码:

'features' => array(
    'label' => 'Zusatz',
    'config' => array(
        'type' => 'select',
        'renderType' => 'selectMultipleSideBySide',
        'size' => 10,
        'minitems' => 0,
        'maxitems' => 999,
        'items' => array(
            array(
                'Parkplätze',
                'parking'
            ),
            array(
                'Freies Wlan',
                'wlan'
            ),
        )
    )
),

它在后端工作正常!

但是,我现在怎样才能正确读取数据呢?我现在不适合域/模型的正确方法。

/**
 * Features
 *
 * @var string
 */
protected $features = '';

/**
 * Returns the features
 *
 * @return string $features
 */
public function getFeatures() {
    return $this->features;
}

/**
 * Sets the features
 *
 * @param string $features
 * @return void
 */
public function setFeatures($features) {
    $this->features = $features;
}

调试代码推出:features => 'parking,wlan' (12 chars)

对于每个不起作用:

<f:for each="{newsItem.features}" as="featuresItem">
    {featuresItem}<br />
</f:for>

感谢帮助!

4

1 回答 1

4

您需要用逗号分解字符串,这样您就可以在循环中迭代它们,至少有两种方法:一种是自定义 ViewHelper,第二种(如下所述)是模型中的瞬态字段,当您将获取功能的 ID,您还需要将其“翻译”为人类可读的标签......

在包含特征的模型中,添加带有getter 的瞬态字段,如下例所示:(当然,您可以删除注释中这些无聊的注释,但您必须保持该@var行的类型正确!):

/**
 * This is a transient field, that means, that it havent
 * a declaration in SQL and TCA, but allows to add the getter,
 * which will do some special jobs... ie. will explode the comma
 * separated string to the array
 *
 * @var array
 */
protected $featuresDecoded;

/**
 * And this is getter of transient field, setter is not needed in this case
 * It just returns the array of IDs divided by comma
 *
 * @return array
 */
public function getFeaturesDecoded() {
    return \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->features, true);
}

如前所述,您需要为 ID 获取人类可读的标签,即使您不构建多语言页面翻译文件也非常有用,只需在文件typo3conf/ext/yourext/Resources/Private/Language/locallang.xlf中为您在 TCA 中选择的每个功能添加项目:

<trans-unit id="features.parking">
    <source>Parkplätze</source>
</trans-unit>
<trans-unit id="features.wlan">
    <source>Freies Wlan</source>
</trans-unit>

如您所见,点后的跨单元的 id 与 TCA 中的功能键相同,

最后,您需要在视图中使用它是迭代瞬态字段而不是原始字段:

<f:for each="{newsItem.featuresDecoded}" as="feature">
    <li>
        Feature with key <b>{feature}</b>
        it's <b>{f:translate(key: 'features.{feature}')}</b>
    </li>
</f:for>

在模型中添加新字段(即使它们是临时的)并在本地化文件中添加或更改条目后,您需要清除系统缓存!在 6.2+ 中,此选项在安装工具中可用(但您可以通过Flash 图标下的 UserTS 将其设置为可用)。

注意:使用自定义 ViewHelper 可以完成相同的操作,但恕我直言,瞬态字段更容易。

于 2016-02-22T19:37:54.887 回答