We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10357
    • 573 Posts
    Is is possible to add a row to a migx grid using a formit hook?
      • 4172
      • 5,888 Posts
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 10357
        • 573 Posts
        Thanks Bruno, that looks like just what I am after. I can't seem to get it working for a doc ID that is provided by the form:

        Formit Call

        [[!FormIt? 
         &hooks=`email,updatePlaces,createOrder,redirect` 
         &emailTpl=`bookingFormReport` 
         &emailTo=`[email protected]` 
         &redirectTo=`9`
        ]]


        Hidden docID field
        <input type="hidden" name="pageID" value="[[+fi.pageID]]"/>


        CreateOrder snippet

        <?php
        $docID = $hook->getValue('pageID');
        $page = $modx->getObject('modResource', $docID);
        
        $value = $page->getTVValue('attendees');
        $items = $modx->fromJSON($value);
         
        $next_id = 1;
        //get the next MIGX_id
        if (is_array($items)){
            foreach ($items as $item){
                $id = $modx->getOption('MIGX_id',$item,0)+1;
                if ($id > $next_id){
                    $next_id = $id;
                }   
            }
        }else{
            $items = array();
        }
          
        $item = array(
            'MIGX_id'       => $next_id,
            'name'          => $hook->getValue('name'),
        );
        $items[] = $item;
          
        if (!$page ->setTVValue('attendees', $modx->toJson($items))) {
          $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
          return false;
        }



        my attendees migx was built with the Configurator and just has one field called name. I have created a tv called attendees with the migxConfig set correctly.



        [ed. note: g5604 last edited this post 11 years, 10 months ago.]
          • 4172
          • 5,888 Posts
          where is your [[+fi.pageID]] coming from?

          This is normally a placeholder setted by formit, which was coming from a POST-request.
          Does it have the correct value before sending the form?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 10357
            • 573 Posts
            pageID comes from formit retriever. It does have the correct value - from the source:

            <input type="hidden" name="pageID" value="15"/>
              • 4172
              • 5,888 Posts
              hmm... then I can't see anything, what should be wrong.
              did you try to log $docID ?

              you can also echo or print_r and die() for debugging in a hook
              or write to a debug-chunk.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 10357
                • 573 Posts
                yes, the log returns 15.
                  • 4172
                  • 5,888 Posts
                  and what exactly does not work?
                    -------------------------------

                    you can buy me a beer, if you like MIGX

                    http://webcmsolutions.de/migx.html

                    Thanks!
                    • 10357
                    • 573 Posts
                    ok, it works now! I just had to return true on success:

                    if (!$page ->setTVValue('orderList', $modx->toJson($items))) {
                      $modx->log(modX::LOG_LEVEL_ERROR,'There was a problem saving your data!');
                      return false;
                    }else {
                        return true;
                    }
                    }


                    Migx just keeps getting better and better, this opens up many possibilities for me.