Created
May 18, 2015 12:01
-
-
Save smichaelsen/00861657d3c2ffe8eaee to your computer and use it in GitHub Desktop.
FlexformViewHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Smichaelsen\MyExtension\ViewHelpers\Format; | |
use TYPO3\CMS\Core\Utility\ArrayUtility; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper; | |
/** | |
* Usage example | |
* | |
* <c:format.flexform data="{page.tx_fed_page_flexform}" field="settings.teaserText"/> | |
* | |
* or | |
* | |
* <c:format.flexform field="settings.teaserText">{page.tx_fed_page_flexform}</c:fomat.flexform> | |
*/ | |
class FlexformViewHelper extends AbstractViewHelper { | |
public function initializeArguments() { | |
parent::initializeArguments(); | |
$this->registerArgument('data', 'string', 'Flexform XML string', FALSE); | |
$this->registerArgument('field', 'string', 'Field to read from the XML', TRUE); | |
} | |
/** | |
* | |
*/ | |
public function render() { | |
$data = $this->renderChildren(); | |
if (empty($data)) { | |
$data = $this->arguments['data']; | |
} | |
/** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */ | |
$objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); | |
$fluxService = $objectManager->get('FluidTYPO3\\Flux\\Service\\FluxService'); | |
$dataArray = ArrayUtility::flatten($fluxService->convertFlexFormContentToArray($data)); | |
if (!isset($dataArray[$this->arguments['field']])) { | |
return NULL; | |
} | |
return $dataArray[$this->arguments['field']]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment