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
    Hi guys - anyone any ideas why this plugin code might be throwing Server 500 errors when trying to use it?

    $parent = $resource->get('parent');
     
    switch($parent) {
        case 22:
           $tv = 'AwardItem_blurb';
           break;
        default:
           return '';
           break;
    }
     
    $v = $resource->getTVValue($tv);
    $resource->setContent($v);
    $resource->save();
    return '';
    
      • 3749
      • 24,544 Posts
      What event is your plugin connected to? Make sure it's not connected to more than one.
        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 28, 2015, 01:21 AM
        What event is your plugin connected to? Make sure it's not connected to more than one.

        Hi Bob, its just connected to OnDocFormSave as per your suggestion... Seems an odd one??? Anyway to debug the issue? Error log seems empty of relevant issues...?
          • 3749
          • 24,544 Posts
          No sure what's wrong. Possibly getTVValue() is returning NULL in some cases and saving NULL in the content field is the issue. Let's try it a different way:


          $parent = (int) $resource->get('parent');
          
          if ($parent == 22) {
              $v = $resource->getTVValue('AwardItem_blurb');
              $v = empty($v) ? ' ' : $v;
              if ($v) {
                  $resource->setContent($v);
                  $resource->save();
              }
          }
          
          return '';
          

          [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 - still getting Server 500 errors in Firebug on save of a resource with the plugin enabled...

            Again, nothing in the MODx error log though - just never ending save loop...

            Not sure if this is a local issue to me or the code is still not right? Have you had it working your end? [ed. note: dubbs last edited this post 11 years, 6 months ago.]
              • 3749
              • 24,544 Posts
              Sorry, typo in the code.

              Line 5 should be:

                  $v = empty($v) ? ' ' : $v;



              (fixed above)
                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 30, 2015, 11:53 AM
                Sorry, typo in the code.

                Line 5 should be:

                    $v = empty($v) ? ' ' : $v;



                (fixed above)

                Bob - you are a leg-end - no server error, saved, and then on refresh - bang the content field is populated with the contents of the specified TV wink Thanks - so just to check, to add more if statements it would look like this yeh?

                $parent = (int) $resource->get('parent');
                 
                if ($parent == 22) {
                    $v = $resource->getTVValue('AwardItem_blurb');
                    $v = empty($v) ? ' ' : $v;
                    if ($v) {
                        $resource->setContent($v);
                        $resource->save();
                    }
                }
                
                if ($parent == 30) {
                    $v = $resource->getTVValue('AnotherTV_blurb');
                    $v = empty($v) ? ' ' : $v;
                    if ($v) {
                        $resource->setContent($v);
                        $resource->save();
                    }
                }
                 
                return '';
                
                  • 3749
                  • 24,544 Posts
                  That looks fine. If there are going to be a bunch of them, I would do it this way:


                  $parent = (int) $resource->get('parent');
                  
                  $resourceTvs = array(
                      22 => 'AwardItem_blurb',
                      30 => 'AnotherTV_blurb',
                  );
                  
                  if (isset($resourceTvs[$parent])) {
                  
                      $v = $resource->getTVValue($resourceTvs[$parent]);
                      $v = empty($v) ? ' ' : $v;
                      if ($v) {
                          $resource->setContent($v);
                          $resource->save();
                      }
                  
                  }
                  
                  return '';
                  


                  It avoids duplicating the code, it will be faster, and you only have to change the array to add more. It won't work, though, if there's more than one TV involved for a resource.
                  [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
                    Quote from: BobRay at Mar 30, 2015, 04:32 PM
                    That looks fine. If there are going to be a bunch of them, I would do it this way:


                    $parent = (int) $resource->get('parent');
                    
                    $resourceTvs = array(
                        22 => 'AwardItem_blurb',
                        30 => 'AnotherTV_blurb',
                    );
                    
                    if (isset($resourceTvs[$parent])) {
                    
                        $v = $resource->getTVValue($resourceTvs[$parent]);
                        $v = empty($v) ? ' ' : $v;
                        if ($v) {
                            $resource->setContent($v);
                            $resource->save();
                        }
                    
                    }
                    
                    return ''
                    


                    It avoids duplicating the code, it will be faster, and you only have to change the array to add more. It won't work, though, if there's more than one TV involved for a resource.

                    BOB!!!! it all works! only tweak was to add the missing ; to the return'' code - updated and working example below:

                    $parent = (int) $resource->get('parent');
                     
                    $resourceTvs = array(
                        22 => 'AwardItem_blurb',
                        30 => 'AnotherTV_blurb',
                    );
                     
                    if (isset($resourceTvs[$parent])) {
                     
                        $v = $resource->getTVValue($resourceTvs[$parent]);
                        $v = empty($v) ? ' ' : $v;
                        if ($v) {
                            $resource->setContent($v);
                            $resource->save();
                        }
                     
                    }
                     
                    return '';
                    


                    Thanks very much for your assistance here - a true MODx genius once more! wink
                      • 3749
                      • 24,544 Posts
                      Glad I could help. smiley
                        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