Search code examples
typo3typoscripttypo3-9.x

Typoscript modify a field:variable


I have this typoscript:

tt_content.gridelements_pi1.20.10.setup {

    3cols.outerWrap = <div>|</div>
    3cols.outerWrap.override.insertData = 1
    3cols.outerWrap.override = <div id="{field:tx_cewrap_id_input}" class="{field:tx_cewrap_class_input} {field:tx_cewrap_class_select}">|</div>
    3cols.outerWrap.override.if.isTrue.field = tx_cewrap_active
}

Which makes sure a wrapper is made around a certain element. The following html is generated as an example:

<div id="" class="full-box full-box-features container pt-75,pb-75"></div>

As you can see there is a comma separated string inserted as "tx_cewrap_class_select". With the {field:tx_cewrap_class_select} part:

pt-75,pb-75

But i want the comma to be a space character so the classes work in html

Now I know about the split option

But how do I fix the code, just need somehow to remove the comma! That's it :)

Thanks in advance for any response I can use.


Solution

  • You can split by comma and join with a space, but in this case it might be easier just to replace the comma by space:

    10 = TEXT
    10.replacement {
        1 {
           search = ,
           replace.char = 32
        }
    }
    

    And here the solution with split. it should be obvious why not to use:

    10 = TEXT
    10.split {
        token = ,
        cObjNum = 1 || 2
        1.current = 1
        2.current = 1
        2.noTrimWrap = | ||
    }
    

    Hint:
    on TEXT you can use stdWrap functions immediately,
    in other context you might need an explicit .stdWrap:

    10.stdWrap.replacement {
    
    10.stdWrap.split {
    

    Either you prepare your values into a register for later use, or you split your override value into a COA. You even might use the replacement on the whole override value in case you are sure no other comma might be needed.

    COA-Solution:
    (don't forget the noTrimWrap for 20 otherwise the classes are appended without space)

    override.cObject = COA
    override.cObject {
        10 = TEXT
        10.value <div id="{field:tx_cewrap_id_input}" class="{field:tx_cewrap_class_input} 
        10.insertData = 1
    
        20 = TEXT
        20.field = tx_cewrap_class_select
        20.replacement {
            1 {
               search = ,
               replace.char = 32
            }
            noTrimWrap= | ||
        }
    
        30 = TEXT
        30.value = ">|</div>
    }