TYPO3 add checkbox field to model

删除回忆录丶 提交于 2020-01-07 04:01:13

问题


I have TYPO3 version 7.6.18. I added 'interest' int field to fe_users table.

My TCA:

'interest' => [
    'exclude' => 1,
    'label' => 'Interest',
    'config' => [
        'type' => 'check',
        'default' => '0',
        'items' => [
            ['Mann', 0 ],
            ['Frau', 1],
            ['Paar', 2]
        ]
    ]
],

Help me please. What I must to model, that user can set interest checkbow in frontend in his profile ? How getters and setters method must be look like ? What object I must use, tell me please


回答1:


This is a bit tricky since TYPO3 stores multiple checkboxes as a single integer value bitmask. Thus at some point you'll need to split this combined value again if you want to use it. BTW, your checkbox values are unused since TYPO3 automatically stores all checkboxes as bit 1 or 0 depending on whether they are checked or not.

A simple solution would be mapping this value to an integer in your model and then provide getters for each possible value:

/**
 * @var integer
 */
protected $interests;

/**
 * @return bool
 */
public function isInterestedInMen()
{
    return $this->interests & 0b00000001;
}

/**
 * @return bool
 */
public function isInterestedInWomen()
{
    return $this->interests & 0b00000010;
}

/**
 * @return bool
 */
public function isInterestedInPairs()
{
    return $this->interests & 0b00000100;
}

You could then use $object->isInterestedInPairs() in Extbase or {object.interestedInPairs} in Fluid.

Here's an example how setters could be implemented:

/**
 * @var integer
 */
protected $interests;

/**
 * @param bool
 */
public function setInterestedInMen($interestedInMen)
{
    if ($interestedInMen) {
        $this->interests |= 0b00000001;
    } else {
        $this->interests &= ~0b00000001;
    }
}

To write to these e.g. via Fluid forms you would simply use <f:form.checkbox property="interestedInMen" value="1" />.

But as you can see this quickly becomes unwieldy and very hard to understand thus I'd suggest either creating a separate table and model for the interests which can then easily be maintained in the backend or at least switch to the select field and use string values which are then stored as CSV locally. This can then be mapped to string in the model and passed through explode() to get separate values. But again, I recommend looking at the separate table/model and relation approach.




回答2:


Here you can find an example which is extending FileReference of TYPO3. The behavior is almost the same. In your case its just FrontendUser instead of FileReference: Extending sys_file_reference (FAL)



来源:https://stackoverflow.com/questions/44647114/typo3-add-checkbox-field-to-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!