We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8168
    • 1,118 Posts
    Quote from: BobRay at Mar 19, 2015, 04:00 PM
    I'm guessing here somewhat, but I think the plugin code would be this two-liner (connected to OnParseDocument):

    $content = $modx->resource->getTVValue('ExampleTv');
    return '';


    This would require MODX 2.2 or greater.




    Thanks Bob - I am running 2.3.1 but the code you send doesnt seem to work - I had some joy getting this to work though

    $page = $modx->getObject('modResource', 68);
    $ContentCopied = $page->getTVValue('AwardItem_blurb');
    $modx->resource->set('content',$ContentCopied);
    


    But.... This now make EVERY resource in the site make the [[*content]] field the same as the contents of the [[*AwardItem_blurb]] TV for resource ID 68... How can I specify that this plugin should only work on resources of children ID 10 for example and pull the contents of [[*AwardItem_blurb]] TV for the resource it is being applied too?
      • 3749
      • 24,544 Posts
      Do you want to have it mirror the TV temporarily, or is this permanent. That code makes a permanent change, in which case you might as well connect it to OnDocFormSave where it won't slow down page loads. I think this would do it (if I'm understanding what you're trying to do):


      if ($resource->get('id') == 68)) {
          $v = $resource->getTVValue('AwardItem_blurb');
          $resource->set('content', $v);
          $resource->save();
      }
      return '';
      


      Be careful, though. Without the test at the top, it will trash the content field of every resource you save.
        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
        • 8168
        • 1,118 Posts
        Quote from: BobRay at Mar 19, 2015, 10:46 PM
        Do you want to have it mirror the TV temporarily, or is this permanent. That code makes a permanent change, in which case you might as well connect it to OnDocFormSave where it won't slow down page loads. I think this would do it (if I'm understanding what you're trying to do):


        if ($resource->get('id') == 68)) {
            $v = $resource->getTVValue('AwardItem_blurb');
            $resource->set('content', $v);
            $resource->save();
        }
        return '';
        


        Be careful, though. Without the test at the top, it will trash the content field of every resource you save.

        Thanks Bob. Its a permanent thing - on the pages I want this applied too - they have no content in the [[*content]] field - Quick question on the code you have suggested - how can I make it so that it works to all the childern of resource id 20 for example. I can then have multiple IFs I guess to say if child resource of resource ID 20 then use TV1, if child of resource ID 30 then use TV2 etc etc...
          • 3749
          • 24,544 Posts
          Assuming that they are direct children, you can do this:


          $parent = $resource->get('parent');
          
          switch($parent) {
              case 20:
                 $tv = 'Tv1';
                 break;
              case 30:
                 $tv = 'Tv2';
                 break;
              default:
                 return '';
                 break;
          }
          
          $v = $resource->getTVValue($tv);
          $resource->set('content', $v);
          $resource->save();
          return '';
          


          If you have to add more cases, make sure each one has a break; statement. [ed. note: BobRay last edited this post 11 years, 6 months ago.]
            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
            • 8168
            • 1,118 Posts
            Thanks Bob - will give that a go! Leg-end!
              • 8168
              • 1,118 Posts
              Bob - Is this code needed?

              $v = $resource->getTVValue('AwardItem_blurb');
              $resource->set('content', $v);
              $resource->save();
              return '';
              


              It sesms like we are hard coding the v variable in the first line? Looks wrong to me?
                • 3749
                • 24,544 Posts
                Quote from: dubbs at Mar 20, 2015, 05:03 PM
                Bob - Is this code needed?

                $v = $resource->getTVValue('AwardItem_blurb');
                $resource->set('content', $v);
                $resource->save();
                return '';
                


                It seems like we are hard coding the v variable in the first line? Looks wrong to me?

                It's needed, but you're correct that it's wrong. It should be:

                $v = $resource->getTVValue($tv);
                $resource->set('content', $v);
                $resource->save();
                return '';


                And of course the TV names in the switch statement ('Tv1, Tv2') should be the actual names of the TVs.
                  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
                  • 8168
                  • 1,118 Posts
                  Bob - Can I just check this code is correct and wont screw up my site before giving it a go?

                  $parent = $resource->get('parent');
                   
                  switch($parent) {
                      case 22:
                         $tv = 'AwardItem_blurb';
                         break;
                      default:
                         return '';
                         break;
                  }
                   
                  $v = $resource->getTVValue($tv);
                  $resource->set('content', $v);
                  $resource->save();
                  return '';
                  


                  With the plugin set to work onDocFormSave

                  So, this will work to ALL children of the resource ID 22, populating the [[*content]] field with identical content from the specified TV - in this case the [[*AwardItem_blurb]].

                  Can I check this wont overwrite anything else etc?

                  This plugin will function everytime a resource within container 22 is saved yeh? Should the update be visible in the "Content" field after saving?

                  Cheers Bob.
                    • 3749
                    • 24,544 Posts
                    Yes, it should be safe, but it will only effect immediate children of resource 22 -- not grandchildren or below.

                    The content field of those resources should show the TV value once it has been saved with the plugin enabled. It will permanently overwrite the content field for children of 22, but won't affect other resources. Saving those children will take a tiny bit longer, but I doubt if you'll notice the difference.

                    This line:

                    $resource->set('content', $v);


                    Should probably be:

                    $resource->setContent($v);


                    Both versions will work, but the second one is the preferred method.
                      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
                      • 8168
                      • 1,118 Posts
                      Thanks Bob - Afraid to report - I get a server 500 error when trying to save a child resource from container ID 22 with the plugin enabled... Not sure what the error is being caused by - but I just see a never ending SAVING progress bar - looking at Firebug this is what is reported...

                      POST http://domain.com/connectors/index.php 500 (Internal Server Error)tiny_mce.js?v=231pl:1 k.create.render.p.onBeforeRenderUI.add.s.submitext-all.js:21 Ext.extend.doFormUploadext-all.js:21 Ext.extend.requestext-all.js:21 Ext.extend.runext-base.js:21 (anonymous function)
                      ext-all.js:21 Uncaught #<Object>ext-all.js:21 Ext.extend.readindex.php?f=/manager/assets/modext/core/modx.localization.js,/manager/assets/modext/util/utilities.…:8 Ext.override.handleResponseext-all.js:21 Ext.form.Action.processResponseext-all.js:21 Ext.extend.successext-all.js:21 oext-all.js:21 sext-all.js:21 I
                      


                      Any ideas???