FLUID 中的 TYPO3 TCA 类型 select?
TYPO3 TCA type select in FLUID?
我在 renderType = selectMultipleSideBySide
中将 TCA 类型 select 用于 T3 后端
这里是 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'
),
)
)
),
它在后端运行良好!
但是,现在如何正确读取数据呢?
我现在不知道 Domain/Model.
的正确方法
/**
* 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>
感谢帮助!
你需要用逗号分解字符串,这样你就可以在循环中迭代它们,至少有两种方法:一种是自定义 ViewHelper,第二种(如下所述)是 transient 字段在你的模型中,当你得到特征的 ID 时,你还需要 "translate" 它到人类可读的标签...
在包含这些特征的模型中,添加瞬态场 with 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 中拥有的功能 select:
<trans-unit id="features.parking">
<source>Parkplätze</source>
</trans-unit>
<trans-unit id="features.wlan">
<source>Freies Wlan</source>
</trans-unit>
如您所见,点后的跨单位 ID 与 TCA 中的特征键相同,
最后,您需要在视图中使用它来迭代 transient 字段而不是原始字段:
<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 可以完成完全相同的事情,但恕我直言,瞬态字段更容易。
我在 renderType = selectMultipleSideBySide
中将 TCA 类型 select 用于 T3 后端这里是 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'
),
)
)
),
它在后端运行良好!
但是,现在如何正确读取数据呢? 我现在不知道 Domain/Model.
的正确方法/**
* 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>
感谢帮助!
你需要用逗号分解字符串,这样你就可以在循环中迭代它们,至少有两种方法:一种是自定义 ViewHelper,第二种(如下所述)是 transient 字段在你的模型中,当你得到特征的 ID 时,你还需要 "translate" 它到人类可读的标签...
在包含这些特征的模型中,添加瞬态场 with 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 中拥有的功能 select:
<trans-unit id="features.parking">
<source>Parkplätze</source>
</trans-unit>
<trans-unit id="features.wlan">
<source>Freies Wlan</source>
</trans-unit>
如您所见,点后的跨单位 ID 与 TCA 中的特征键相同,
最后,您需要在视图中使用它来迭代 transient 字段而不是原始字段:
<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 可以完成完全相同的事情,但恕我直言,瞬态字段更容易。