Adrian Dymorz
Adrian Dymorz

Reputation: 967

Missing child nodes in view helper

I created a view helper which used to work on TYPO3 7.6 but stopped working on TYPO3 8.7. It always renders the else part even though the return value of evaluateCondition is correct while debugging.

While debugging I see that AbstractConditionViewHelper in AbstractConditionViewHelper gets called and from there renderThenChild or renderElseChild get called. But there the child nodes are missing.

My view helper:

<?php

namespace Vendor\Extkey\ViewHelpers;

class IfRegExpViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper
{

    /**
     * Initialize arguments
     * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
     */
    public function initializeArguments() {
        $this->registerArgument('then', 'mixed', 'Value to be returned if the condition if met.', false);
        $this->registerArgument('else', 'mixed', 'Value to be returned if the condition if not met.', false);
        $this->registerArgument('value', 'string', 'The value', TRUE);
        $this->registerArgument('pattern', 'string', 'The regex pattern to check', TRUE);
    }

    /**
     * Check if pattern matches
     *
     * @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper, allows for flexiblity in overriding this method.
     * @return string the rendered string
     * @api
     */
    protected static function evaluateCondition($arguments = NULL)
    {
        if (preg_match($arguments['pattern'], $arguments['value']) === 1) {
            return true;
        } else {
            return false;
        }
    }
}

Usage in template:

<f:if condition="{eddaylight:IfRegExp(value: overview, pattern: '/[a-zA-Z0-9]+/')}">
    <f:then>
        {overview}
    </f:then>
    <f:else>
        something else
    </f:else>
</f:if>

Is there something I need to do that the child nodes get parsed?

Upvotes: 0

Views: 217

Answers (1)

Claus Due
Claus Due

Reputation: 4261

<f:if condition="{eddaylight:IfRegExp(value: overview, pattern: '/[a-    zA-Z0-9]+/')}">
    <f:then>
        {overview}
    </f:then>
    <f:else>
        something else
    </f:else>
</f:if>

Is wrong - the ViewHelper you have written is a condition ViewHelper itself, so it returns either the value of the then or else node/closure/argument and if none are specified, nothing is returned. What you've written above is the same as:

<f:if condition="">

Which is obviously false.

The way to use your ViewHelper is:

<eddaylight:ifRegExp value="{overview}" pattern="/[a-zA-Z0-9]+/">
    <f:then>
        {overview}
    </f:then>
    <f:else>
        something else
    </f:else>
</eddaylight:ifRegExp>

Same applies to every other subclass of the AbstractConditionViewHelper.

Note about childnodes:

  • In your current use case there rightfully are no child nodes since you write the ViewHelper in inline notation without passing a child value.
  • If you expand to the correct syntax you will see child nodes, but only until the template gets compiled, at which time child nodes will no longer be set.

Upvotes: 3

Related Questions