Search code examples
typo3fluidextbasebitmasktypo3-extensions

TYPO3: Reading the values of a TCA 'type' => 'check' (bitmask)


I need to show a selection of days in an event in the frontend:

in my TCA I set the field like this:

'days' => [
    'exclude' => true,
    'label' => 'choose weekdays',
    'config' => [
        'type' => 'check',
        'eval' => 'required,unique',
        'items' => [
            ['monday',''],
            ['thuesday',''],
            ['wednesday',''],
            ['thursday',''],
            ['friday',''],
            ['saturday',''],
            ['sunday',''],
        ],
        'cols' => 'inline',
    ],
],

That stores an integer in the db, but now I have to display the selected days in a fluid template in the frontend.

This is the reference regarding in the TYPO3 documentation which explains that I should check the bit-0 of values ... I've searched a lot but couldn't find anything except this question here on stack overflow, which I cannot get to work.


Solution

  • I strongly recommend not to use the bitmasking feature of the check field. It's rarely worth the overhead to split the values apart again and also is a lot harder to understand for most developers.

    Instead you can use a select field, in this case selectCheckBox should serve you well. Given a static list of items you will get a CSV string with the selected values which is a lot easier to split, e.g. in a getter method of an Extbase domain model. If it makes sense you can even use a relation to records instead which is even cleaner but requires additional work.

    If you still want to continue with bitmasks this answer may help you.