SHARE
TWEET

FlexFormDataProcessor

einpraegsam Jun 10th, 2016 424 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace In2code\In2template\DataProcessor;
  3.  
  4. use In2code\In2template\Utility\ArrayUtility;
  5. use TYPO3\CMS\Core\Utility\GeneralUtility;
  6. use TYPO3\CMS\Extbase\Service\TypoScriptService;
  7. use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
  8. use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
  9.  
  10. /**
  11.  * Class FlexFormProcessor
  12.  */
  13. class FlexFormProcessor implements DataProcessorInterface
  14. {
  15.  
  16.     /**
  17.      * @var \TYPO3\CMS\Extbase\Service\TypoScriptService
  18.      */
  19.     protected $typoScriptService;
  20.  
  21.     /**
  22.      * Construct
  23.      */
  24.     public function __construct()
  25.     {
  26.         $this->typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
  27.     }
  28.  
  29.     /**
  30.      * @param ContentObjectRenderer $cObj The data of the content element or page
  31.      * @param array $contentObjectConfiguration The configuration of Content Object
  32.      * @param array $processorConfiguration The configuration of this processor
  33.      * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
  34.      * @return array the processed data as key/value store
  35.      */
  36.     public function process(
  37.         ContentObjectRenderer $cObj,
  38.         array $contentObjectConfiguration,
  39.         array $processorConfiguration,
  40.         array $processedData
  41.     ) {
  42.         $originalValue = $processedData['data']['pi_flexform'];
  43.         $flexForm = $this->convertFlexFormContentToArray($originalValue);
  44.         $data = [
  45.             'data' => $cObj->data,
  46.             'settings' => $this->typoScriptService->convertTypoScriptArrayToPlainArray(
  47.                 (array)$contentObjectConfiguration['variables.']['settings.']
  48.             )
  49.         ];
  50.         $data['settings'] = array_merge($data['settings'], $flexForm['settings']);
  51.         return $data;
  52.     }
  53.  
  54.     /**
  55.      * Convert XML to FlexForm
  56.      *      Note: flexFormService->convertFlexFormContentToArray() seems to be broken for xml of facultymenu
  57.      *
  58.      * @param string $xml
  59.      * @return array
  60.      */
  61.     protected function convertFlexFormContentToArray($xml)
  62.     {
  63.         $array = GeneralUtility::xml2array($xml);
  64.         $flexForm = [];
  65.         foreach ((array) $array['data'] as $sheetTitle => $sheet) {
  66.             foreach ((array) $sheet['lDEF'] as $fieldTitle => $field) {
  67.  
  68.                 // simple
  69.                 if (empty($field['el']) && !empty($field['vDEF'])) {
  70.                     $flexForm[$fieldTitle] = $field['vDEF'];
  71.  
  72.                     // advanced
  73.                 } elseif (!empty($field['el'])) {
  74.                     foreach ((array) $field['el'] as $number => $fieldConfiguration) {
  75.                         foreach ((array) array_keys($fieldConfiguration) as $key) {
  76.                             if ($key[0] === '_') {
  77.                                 continue;
  78.                             }
  79.                             foreach ((array) $fieldConfiguration[$key]['el'] as $fieldTitle2 => $field2) {
  80.                                 $flexForm[$fieldTitle][$number][$key][$fieldTitle2] = $field2['vDEF'];
  81.                             }
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.         return ArrayUtility::resolveArrayPathsRecursive($flexForm);
  88.     }
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top