Bug #50555

Can't get an empty array in if condition

Added by Thomas Christiansen about 2 years ago. Updated about 2 years ago.

Status:Closed Start date:2013-07-30
Priority:Should have Due date:
Assigned To:Bastian Waidelich % Done:

0%

Category:-
Target version:-
Has patch:No Affected Flow version:(any)

Description

Hi all,
i am having trouble to get an empty array in a shorthand if condition.

The situation is the following:
I have an Input field which can be required or optional, depending on a checkbox in Backend.

In my Template i tried this;

additionalAttributes="{f:if(condition: '{field.mandatory}', then: '{required: 1}', else: '{}')}"

The "then"-case works fine, i am getting the attribute required="1" as expected.

The problem is, that the '{}' in the else-case returns an empty string, not an empty array. Then i get an exception, because additionalAttributes expects an array.
I tried a couple of things to get this to work like {0: null}, {\'\'} or {null} but i always get either an empty string or an non-empty array like array(0 => null).
The last one is not an option because it would result in an attribute "0" in the html tag.

Me and my colleagues came up with a view workarrounds:

1) Assign an empty array to a variable in the controller and use this variable in the template.
- This is in my case not an option because i am extending an TER extension and dont want to override its controller.

2) Add an "Empty-Array-Getter" to the Model like getEmptyArray which returns an empty array.
- This is a possibility, because i am extending the model as well, but... urghs ;)

3) Write a viewhelper that adds an empty array to the templateVariableContainer.
- This is probably the best workaround and i am using this for now.

The viewhelper looks simply like this:

class EmptyArrayViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
public function render() {
$this->templateVariableContainer->add('emptyArray', array());
return NULL;
}
}

So i use can {emptyArray} in my template now after calling the viewhelper:
<fx:EmptyArray />
...
additionalAttributes="{f:if(condition: '{field.mandatory}', then: '{required: 1}', else: '{emptyArray}')}"

Did we miss anything here or is there no other way to solve this scenario?

Thanks and regards,
Thomas

History

#1 Updated by Bastian Waidelich about 2 years ago

  • Status changed from New to Needs Feedback
  • Assigned To set to Bastian Waidelich

What about

1<f:if condition="{field.mandatory}">
2  <f:then>
3    <f:form.checkbox property="foo" additionalAttributes="{required: true}" />
4  </f:then>
5  <f:else>
6    <f:form.checkbox property="foo" />
7  </f:else>
8</f:if>

If you need it more than once you could put that code into a partial and use it like this:

1  <f:render partial="checkbox" arguments="{requiredCondition: field.mandatory}" />

(and then change {field.mandatory} to {requiredCondition} in the partial)

#2 Updated by Thomas Christiansen about 2 years ago

Yes sure, that is another workaround.

Actually this is where I started and what i did not want because its annoying to maintain.

Thanks for your reply but i am going with the viewhelper solution.

Regards,
Thomas

#3 Updated by Bastian Waidelich about 2 years ago

  • Status changed from Needs Feedback to Closed

Thomas Christiansen wrote:

Thanks for your reply but i am going with the viewhelper solution.

Ok, I'm closing the issue for now because that is actually a missing feature that would be quite hard to implement without huge benefit.

Some background information:
The Fluid argument notation is a fully compatible subset of the JavaScript Object Notation (JSON). For an empty array we'd need to add support for a new symbol:

[]

This would increase complexity and create conflicts with existing Fluid templates.

Feel free to add comments or re-open if you don't agree

Also available in: Atom PDF