On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • ctownsend23 Reply #1, 5 years ago
    So I've extended the modResource to add a few more fields.

    Now, I'm trying to determine how to add a form to the front end of the site to allow users to add a new resource including the additional fields. I've toyed with formit2resource and it does just that. It will add a new resource, however how do I get it to create the pagetitle, alias, content, etc. and also my new extended fields which are in a different table?

    I've considered a two part form. Using formit2resource to add the initial new resource and then formit retriever to get the ID of the new resource and continue on with something like formit2db on the second part of the form. The problem is retrieving the ID from the new resource doesn't really work as expected.

    Anyone have experience with this? Any thoughts on where to look for more info?
    • donshakespeare Reply #2, 5 years ago
      Is NewsPublisher an option?
        TinymceWrapper: Complete back/frontend content solution.
        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
      • you just need to extend formit2resource a bit, if you want to save everything at once

        do you have a link to the formit2resource - code, which you are using currently?

        maybe the hook-snippet could also be modified to use runProcessor('resource/create') instaed of xpdo directly

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

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
        • ctownsend23 Reply #4, 5 years ago
          Bruno,
          I do not currently have a link to the formit2resource code. I actually just pulled it from here:

          http://forums.modx.com/thread/69981/formit-formit2resource

          I just need to add a couple of additional fields that are in a separate table. I'm not sure how to go about adding the pagetitle, content, etc. and then also adding the extended fields along with the ID from the newly created resource in order for it to link the two different tables.

          Quote from: Bruno17 at Jul 23, 2015, 05:50 PM
          you just need to extend formit2resource a bit, if you want to save everything at once

          do you have a link to the formit2resource - code, which you are using currently?

          maybe the hook-snippet could also be modified to use runProcessor('resource/create') instaed of xpdo directly

          • something like that should work:

            <?php
            
            $doc = $modx->getObject('modResource', array('id' => $hook->getValue('resource_id')));
            
            if (empty($doc)) {
                $doc = $modx->newObject('modResource');
                $doc->set('createdby', $modx->user->get('id'));
            } else {
                $doc->set('editedby', $modx->user->get('id'));
            }
            
            $allFormFields = $hook->getValues();
            foreach ($allFormFields as $field => $value) {
            
                if ($field !== 'spam' && $field !== 'resource_id') {
                    $doc->set($field, $value);
                }
            }
            
            $doc->set('template', '1');
            //set other doc-fields here
            
            if ($doc->save()) {
            
                $id = $doc->get('id');
                // set fields of extented table here
                $classname = 'yourExtClass';
            
                $ext_object = $modx->newObject($classname);
                $ext_object->set('resource_id', $id);// set the relation-field here
                $ext_object->set('yourField', $hook->getValue('yourField'));
                $ext_object->save();
            
            }
            
            /*
            foreach ($allFormFields as $field=>$value)
            {
            if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
            {
            // handles checkboxes & multiple selects elements 
            if (is_array($value)) {
            $featureInsert = array();
            while (list($featureValue, $featureItem) = each($value)) {
            $featureInsert[count($featureInsert)] = $featureItem;
            }
            $value = implode('||',$featureInsert);
            }   
            $tv->setValue($doc->get('id'), $value);
            $tv->save();
            }
            }
            */
            
            return true;
            
            
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
            • ctownsend23 Reply #6, 5 years ago
              Thanks Bruno!
              I'll give this a try. So it seems from my limited programming knowledge that the snippet saves the resource, retrieves the id and then does a second save?
              • ctownsend23 Reply #7, 5 years ago
                Bruno,
                I'm assuming additional extended fields are added as so?

                 $ext_object = $modx->newObject($classname);
                    $ext_object->set('resource_id', $id);// set the relation-field here
                    $ext_object->set('yourField1', $hook->getValue('yourField1'));
                $ext_object->set('yourField2', $hook->getValue('yourField2'));
                $ext_object->set('yourField3', $hook->getValue('yourField3'));
                $ext_object->set('yourField4', $hook->getValue('yourField4'));
                    $ext_object->save();
                • Have you tried NewsPublisher? I think it can work for this.
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                  • ctownsend23 Reply #9, 5 years ago
                    Bob,
                    No I've never worked with newspublisher. My concern is going to be in attaching files in a future update of my current project. I'm not adverse to using it as long as it has capabilities for future expansion. I'll look at it.
                    • NP won't let you attach files without some custom addition to the form, but whatever you would use with FormIt should work there as well. NP fires the OnDocFormSave event, which might make it easier.

                      It might be easier yet to use one or more File TVs if that would work for you. NP should handle those fine.
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting