<![CDATA[ Automatically populate a field with Resource ID when adding new item. - My Forums]]> https://forums.modx.com/thread/?thread=102889 <![CDATA[Automatically populate a field with Resource ID when adding new item.]]> https://forums.modx.com/thread/102889/automatically-populate-a-field-with-resource-id-when-adding-new-item#dis-post-554123
My form tabs currently look like this:
[
{"caption":"Info", "fields": [
   {"field":"date","caption":"Date","inputTVtype":"date"},
   {"field":"change","caption":"Change","inputTVtype":"textarea"}
]}
]


And I would like them to look something like this:
[
{"caption":"Info", "fields": [
   {"field":"date","caption":"Date","inputTVtype":"date"},
   {"field":"change","caption":"Change","inputTVtype":"textarea"},
   {"field":"resource-id","caption":"Resource ID","inputTVtype":"text"}
]}
]


Normally I like to show some code that I was trying to work on, but I don't have much. I tried using @EVAL inside the Form Tabs area, but that threw an error. I then tried writing a plugin that populated a TV with the resource ID, which worked, but I couldn't figure out a way to pass that along to MIGX when creating a new entry.

I'm sure the best option would be rebuilding the system in an MIGXdb, but how it's currently functioning makes sense to me (I do use MIGXdb for a lot of other things). Basically, I'm merging all the MIGX TV's from the resources I need using mergeMIGX. I want to include the resource IDs in each entry is so I can include page titles and links in the tpl file to link back to each entry's resource.

This is what mergeMIGX looks like, in case it's relevant:
<?php
// copied from here http://forums.modx.com/thread/?thread=41006&page=20
// [[mergeMIGX? &tvname=`mymigxtv` &docids=`10,11,12` &tpl=`mytpl` &debug=`1`]]
  
$docids = explode(',', $docids);
if ($tv = $modx->getObject('modTemplateVar', array('name' => $tvname))) {
    $allitems = array();
    foreach ($docids as $docid) {
        $outputvalue = $tv->renderOutput($docid);
        $items = $modx->fromJSON($outputvalue);
        if (is_array($items)) {
            $allitems = array_merge($allitems, $items);
        }
        elseif ($debug) {
            echo 'Error in '.$docid.':'.$outputvalue;
        }
  
    }
    $scriptProperties['value'] = $modx->toJSON($allitems);
}
  
return $modx->runSnippet('getImageList', $scriptProperties);


I appreciate any help or any suggestions!]]>
firebot6 Sep 26, 2017, 09:00 AM https://forums.modx.com/thread/102889/automatically-populate-a-field-with-resource-id-when-adding-new-item#dis-post-554123
<![CDATA[Re: Automatically populate a field with Resource ID when adding new item. (Best Answer)]]> https://forums.modx.com/thread/102889/automatically-populate-a-field-with-resource-id-when-adding-new-item#dis-post-554135
$tvname = 'yourMIGXtv';
$items = json_decode($resource->getTVValue($tvname),true);
$newitems = array();
if (is_array($items)){
    foreach($items as $item){
        $item['resource_id'] = $resource->get('id');
        $newitems[] = $item;
    }
    $resource->setTVValue($tvname,json_encode($newitems));
}


]]>
Bruno17 Sep 27, 2017, 05:11 AM https://forums.modx.com/thread/102889/automatically-populate-a-field-with-resource-id-when-adding-new-item#dis-post-554135