Created
December 15, 2015 15:14
-
-
Save exside/a84c81dfedabc50dd934 to your computer and use it in GitHub Desktop.
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 | |
//Make sure to tick 'OnDockFormSave' | |
$migxtv_templates = '2'; | |
$config = '[ | |
{"tvname":"tabbed_content_items","template":"2","target_tv":"search_migx_data","fields":[ | |
{"field":"title"}, | |
{"field":"intro"}, | |
{"field":"cta_link_text"} | |
]} | |
]'; | |
function jsonFieldsToArray($items, $fields) { | |
global $modx; | |
$output = array(); | |
if (!is_array($items)) { | |
$items = $modx->fromJson($items); | |
} | |
if (is_array($items)) { | |
foreach ($items as $item) { | |
foreach ($fields as $field) { | |
$fieldname = $modx->getOption('field', $field, ''); | |
$type = $modx->getOption('type', $field, ''); | |
if (isset($item[$fieldname])) { | |
if ($type == 'migx') { | |
$subitems = $item[$fieldname]; | |
$subfields = $modx->getOption('fields', $field, ''); | |
$suboutput = jsonFieldsToArray($subitems,$subfields); | |
$output = array_merge($output,$suboutput); | |
} else { | |
$output[] = $item[$fieldname]; | |
} | |
} | |
} | |
} | |
} | |
return $output; | |
} | |
$migxtv_templates = explode(',', $migxtv_templates); | |
$resource_template = $resource->get('template'); | |
if (in_array($resource_template, $migxtv_templates)) { | |
$config = $modx->fromJson($config); | |
foreach ($config as $cfg) { | |
$tvname = $modx->getOption('tvname', $cfg, ''); | |
$template = $modx->getOption('template', $cfg, ''); | |
$fields = $modx->getOption('fields', $cfg, ''); | |
$target_tv = $modx->getOption('target_tv', $cfg, ''); | |
$items = $resource->getTVValue($tvname); | |
$values = jsonFieldsToArray($items, $fields); | |
if ($template == $resource_template){ | |
//has the template itself - we save to that resource | |
$resource->setTVValue($target_tv,implode("\n\n",$values)); | |
}elseif ($target_resource = $modx->getObject('modResource',array('template'=>$template,'parent'=>$resource->get('id')))){ | |
//otherwise search child-resource with that template | |
$target_resource->setTVValue($target_tv,implode("\n\n",$values)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment