-->

TYPO3 TCA type select in FLUID?

2020-04-20 08:24发布

问题:

I use for the T3 Backend a TCA type select in a renderType = selectMultipleSideBySide

Here the TCA Code:

'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'
            ),
        )
    )
),

it works fine in the Backend!

But, how can I read the data properly now? I dont now the right way for the 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;
}

the Debug Code put out: features => 'parking,wlan' (12 chars)

a for each dont work:

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

thanks for help!

回答1:

You need to explode the string by comma, so you'll be able to iterate them in the loop, there are at least two approaches: one is custom ViewHelper, second (described bellow) is transient field in your model, when you'll get the IDs of features, you need also to "translate" it to human readable label...

In your model that holds the features, add the transient field with getter like in the sample below: (of course you may delete these boring comment in the annotation but you must to keep the @var line with proper type!):

/**
 * 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);
}

As mentioned before, you need to get human readable labels for the ID, even if you don't build multi-lingual page translation file are very useful, just in the file typo3conf/ext/yourext/Resources/Private/Language/locallang.xlf add items for each feature you have in your TCA select:

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

as you can see id's of the trans-units after the dot are the same as the keys of your features from TCA,

finally all you need to use this in the view is iterating the transient field instead of original one:

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

After adding new fields into model (even if they're transient) and adding or changing entries in localization files you need to clear the System cache! in 6.2+ this option is available in the install tool (but you can set it available via UserTS under the flash icon).

Note: the pretty same thing can be done with custom ViewHelper, but IMHO transient field is easier.