Skip to content

Instantly share code, notes, and snippets.

@t3easy
Created November 8, 2017 14:40
Show Gist options
  • Save t3easy/22bdcf7189e0dce76e246a581790e4a4 to your computer and use it in GitHub Desktop.
Save t3easy/22bdcf7189e0dce76e246a581790e4a4 to your computer and use it in GitHub Desktop.
<?php
namespace Vendor\Extension\DataProcessing;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
/**
* This data processor can be used for processing data for the content elements which have flexform contents in one field
*
* Example TypoScript configuration:
* 10 = Vendor\Extension\DataProcessing\FlexFormProcessor
* 10 {
* if.isTrue.field = pi_flexform
* fieldName = pi_flexform
* as = flexform
* }
*
* whereas "flexform" can be used as a variable {flexform} inside Fluid to fetch values.
* if as = settings, flexform settings are merged with contentObjectConfiguration['settings.']
*
*/
class FlexFormProcessor implements DataProcessorInterface
{
/**
* Process FlexForm field data to an array
*
* @param ContentObjectRenderer $cObj The data of the content element or page
* @param array $contentObjectConfiguration The configuration of Content Object
* @param array $processorConfiguration The configuration of this processor
* @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
* @return array the processed data as key/value store
*/
public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
{
if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
return $processedData;
}
// set targetvariable, default "flexform"
$targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'flexform');
// set fieldname, default "pi_flexform"
$fieldName = $cObj->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform');
// parse flexform
if (is_string($cObj->data[$fieldName])) {
if ($cObj->data[$fieldName] !== '') {
$flexFormService = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\FlexFormService::class);
$processedData[$targetVariableName] = $flexFormService->convertFlexFormContentToArray($cObj->data[$fieldName]);
}
}
// if target variable is settings, merge FlexForm settings prefixed with 'settings.' with contentObjectConfiguration['settings.']
if ($targetVariableName == 'settings' && is_array($processedData[$targetVariableName]['settings'])) {
$processedData[$targetVariableName] = $processedData[$targetVariableName]['settings'];
if (is_array($contentObjectConfiguration['settings.'])) {
$typoScriptService = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Service\TypoScriptService::class);
$flexFormConfiguration = $processedData[$targetVariableName];
$processedData[$targetVariableName] = $typoScriptService->convertTypoScriptArrayToPlainArray($contentObjectConfiguration['settings.']);
ArrayUtility::mergeRecursiveWithOverrule($processedData[$targetVariableName], $flexFormConfiguration);
}
}
return $processedData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment