arraystypo3fluidviewhelpertypo3-7.x

TYPO3 returns "Array" to Fluid as String


I try to pass an array within my Viewhelper to the Fluidtemplate. It always shows the string "Array". If I try to use it as parameter in the f:for each viewhelper, I get an exception because it is a string and not an array. I used Typo3 6.2 before, now I have Typo3 7 and it stopped working.

public function render($uids) { // $uids='901,902,903'
    $uidArray = explode(',', $uids);

    $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
    $repository = $objectManager->get('XXX\\X\\Domain\\Repository\\FooRepository');
    $query = $repository->createQuery();
    $query->getQuerySettings()->setRespectStoragePage(FALSE);
    $query->matching(
        $query->in('uid', $uidArray)
    );
    return $query->execute()->toArray();
}

This is my Fluid template:

{namespace vh=My/Namespace/ViewHelpers}
<f:for each="{vh:GetArray(uids: '901,902,903')}">...</f:for>

Solution

  • You cannot return an array with your viewhelper, because viewhelper always return strings.

    You can however introduce a new variable to the current render context and then use this variable inside your viewhelper.

    public function render() {
      $returnArray = array('a' => 17, 'b' => 42);
      $this->templateVariableContainer->add('returnArray', $returnArray);
      $output = $this->renderChildren();
      $this->templateVariableContainer->remove('returnArray');
      return $output;
    }
    

    Inside your template you can then run a for loop over {returnArray}.