【问题标题】:TYPO3 TCA type select in FLUID?FLUID 中的 TYPO3 TCA 类型选择?
【发布时间】:2016-02-22 18:54:35
【问题描述】:

我在 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>

感谢您的帮助!

【问题讨论】:

  • 我对 extbase 的了解并不深……但我会尝试将功能作为数组返回。然后你可以遍历它们

标签: typo3 fluid typo3-7.6.x


【解决方案1】:

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

在包含特征的模型中,添加瞬态字段 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 选择:

<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 也可以完成相同的操作,但恕我直言,瞬态字段更容易。

【讨论】:

  • jear,很好,非常感谢您的帮助!我重建了我的版本!其他语言我建立了另一种,谢谢!但是调试中的输出是:featuresDecoded =&gt; NULL delete i this code line? /** * Sets the features * * @param string $features * @return void */ public function setFeatures($features) { $this-&gt;features = $features; }
  • 啊,完美,它有效,我首先需要你的代码,然后是我的代码;)
  • 当然你需要在模型中保留你的original字段,transient字段只是temporary一个。模型类中字段和吸气剂的顺序无关紧要
  • 就像受保护的stackoverflow.com/questions/44728952/… 中的建议一样,我建议将protected $featuresDecoded; 更改为$featuresDecoded = []; 或旧的PHP 版本受保护protected $featuresDecoded = array();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多