Sebastian
Sebastian

Reputation: 929

Undeclared arguments passed to ViewHelper maxRange

After updating TYPO3, I get a TYPO3Fluid\Fluid\Core\ViewHelper\Exception "Undeclared arguments passed to ViewHelper ... maxRange Valid arguments are."

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;

class NumberOfStarsViewHelper extends AbstractViewHelper
{
    /**
     * divides the maxRange with two
     * (A rating of 10 results in 5 Starts e.g.)
     *
     * @param integer $maxRange
     * @return boolean
     */
    public function render($maxRange)
    {
        return array_fill(0, ($maxRange / 2), 'iter');
        //===
    }

}

What can i do? Thanks

Upvotes: 0

Views: 617

Answers (2)

Sebastian
Sebastian

Reputation: 929

here the solution:

    use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
    use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
    use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
    
    
    class NumberOfStarsViewHelper extends AbstractViewHelper
    {
        public function initializeArguments() {
            parent::initializeArguments();
            $this->registerArgument('maxRange', 'string', 'divides the maxRange with two', false, null);
        }
    
        use CompileWithRenderStatic;
    
        public static function renderStatic(
            array $arguments,
            \Closure $renderChildrenClosure,
            RenderingContextInterface $renderingContext
        ) {
            return array_fill(0, ($arguments["maxRange"] / 2), 'iter');
        }
    }

Upvotes: 0

Geee
Geee

Reputation: 2243

You're are following old fashin method to create ViewHelper use renderStatic instead of render method. Follow official document here.

For more, @Michael has explained diff. between renderStatic and render!

So, What you will need to do is,

  • Migrate old method to new one (I said above)
  • You will need to register argument(s) (See documentation)

See below refectored code of your ViewHelper:

use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;


class NumberOfStarsViewHelper extends AbstractViewHelper
{
    use CompileWithContentArgumentAndRenderStatic;

    public function initializeArguments() {
        $this->registerArgument('maxRange', 'string', 'divides the maxRange with two', false, null);
    }

    public static function renderStatic(
       array $maxRange,
       \Closure $renderChildrenClosure,
       RenderingContextInterface $renderingContext
    ) {
        $maxRange = $renderChildrenClosure();

        // Debug to get argument
        var_dump($maxRange);
        // return array_fill(0, ($maxRange / 2), 'iter');
        //===
    }
}

I have referred to example from here: https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/8-Fluid/8-developing-a-custom-viewhelper.html#with-renderstatic

For more, check out the documentation here: https://docs.typo3.org/m/typo3/book-extbasefluid/master/en-us/8-Fluid/8-developing-a-custom-viewhelper.html

Upvotes: 1

Related Questions