Skip to content

Instantly share code, notes, and snippets.

@dmitryd
Created July 10, 2013 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryd/5964387 to your computer and use it in GitHub Desktop.
Save dmitryd/5964387 to your computer and use it in GitHub Desktop.
This example shows how to use the 'split' TypoScript function to create a set of checkboxes easily. There are three files total: general code, stdWrap helper and usage example.
temp.checkbox_with_split = COA
temp.checkbox_with_split {
# Define data
10 = LOAD_REGISTER
10 {
# Valid bits in the field
bits = 1,2,3,4,5,6,7
# Field name in the database. Don't move below other registers!
fieldName = tx_myext_field
}
# Split bit values and creates checkboxes for them.
20 = TEXT
20 {
data = register:bits
split {
token = ,
cObjNum = 1
# Checkbox
1 = COA
1 {
# Prepare data
10 = LOAD_REGISTER
10 {
# Bit in the database field (just incrementing for each checkbox)
bit.current = 1
# "checked" attribute of the checkbox
checkedAttr =
checkedAttr.override = checked="checked"
checkedAttr.override {
if.isTrue {
postUserFunc = tx_myext_stdwrap->checkBitValue
postUserFunc {
# Bits are counted from 1!
bit.data = register:bit
fieldName.data = register:fieldName
}
}
}
# Label text of the checkbox
label {
stdWrap {
dataWrap = LLL:EXT:myext/locallang_db.xml:tx_mytable.{register:fieldName}.{register:bit}
wrap3 = {|}
insertData = 1
}
htmlSpecialChars = 1
}
}
# Field
20 = TEXT
20 {
# Assemble the checkbox from registers
dataWrap = <input {register:checkedAttr} id="{register:fieldName}_{register:bit}" name="tx_mext_pi1[{register:fieldName}][]" type="checkbox" value="{register:bit}" /><label for="{register:fieldName}_{register:bit}">{register:label}</label><br />
}
100 = RESTORE_REGISTER
}
}
}
}
<?php
/**
* This class contains a postUserFunc functions for the stdWrap.
*
* (c) 2013 Dmitry Dulepov
*
* @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
*/
class tx_myext_stdwrap implements t3lib_Singleton {
/** @var tslib_cObj */
public $cObj;
/**
* Cheks that the value of the bit 'bit' in the field 'fieldName' matches.
*
* @param string $content
* @param array $conf
* @return string
*/
public function checkBitValue($content, array $conf) {
if (isset($conf['bit.'])) {
$conf['bit'] = $this->cObj->stdWrap($conf['bit'], $conf['bit.']);
}
if (isset($conf['fieldName.'])) {
$conf['fieldName'] = $this->cObj->stdWrap($conf['fieldName'], $conf['fieldName.']);
}
$bit = 1 << max(0, intval($conf['bit']) - 1);
$fieldValue = intval($this->cObj->data[$conf['fieldName']]);
return ($fieldValue & $bit);
}
}
<INCLUDE_TYPOSCRIPT: source="FILE:EXT:myext/static/includes/checkboxes.txt">
plugin.tx_myext_pi1 {
rendering.checkboxes = COA
rendering.checkboxes {
20 < rendering.checkboxes
20 {
20.data = LLL:EXT:myext/locallang_db.xml:tx_mytable.tx_myext_field2
30.10 {
bits = 1,2,3,4,5
fieldName = tx_myext_field2
}
}
30 < rendering.checkboxes
30 {
20.data = LLL:EXT:myext/locallang_db.xml:tx_mytable.tx_myext_field3
30.10 {
bits = 1,2
fieldName = tx_myext_field3
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment