New Community Forums are coming. Watch this space for news.
Subscribe: RSS
  • I have an existing non-MIGXdb setup that I use across multiple resources and I would like to automatically include the resource ID in a field for each new entry.

    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!

    This question has been answered by Bruno17. See the first response.

    • discuss.answer
      you could have a plugin at OnDocFormSave, with something like that:

      $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));
      }
      


        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!