We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    I'm trying to change the parent of a resource based on a value in a tv. I've got this plugin so far:

    <?php
    /* only for update mode  */
    if($mode == modSystemEvent::MODE_NEW) { return; } 
    
    /* only for resources with parent 11 */
    if ($resource->get('parent') == 11)  {
    	$parentFromTv = $resource->getTVValue('new_parent');
    
            /* if a value is set */
    	if ( ($parentFromTv != '') && ($parentFromTv != '-') ) {
    		$resource->set('parent', $parentFromTv);
    		$resource->save();
    		}
    	}
    return;


    I've tried different system events. It's not working on OnDocFormSave. The parent stays '11'.
    On OnDocFormRender it does change the parent, but only if the tv 'new_parent' is set, the resource saved and the page manually reloaded. This is not useful for my client.

    I must be doing something wrong! It should be possible to change the parent, wouldn't it?
    Or maybe I need to refresh the manager page, or send the user to another url after the save. I don't know how to do this. Using sendRedirect is not working for me.

    Thanks for any help!
      • 3749
      • 24,544 Posts
      It looks good to me, though I would suggest this change:

      if ( is_numeric($parentFromTv) ) {
              $resource->set('parent', $parentFromTv);
              $resource->save();
              }


      If might work to just use sendRedirect(). It doesn't always work in plugins. That would look like this:

      $modx->sendRedirect(MODX_MANAGER_URL . '?a=resource/update&id=' . $resource->get('id'));



      If not, you might try deleting the Resource's cache file, either instead of, or before the redirect. This is not the official method, but it should work if you're using the standard setup for the MODX cache:

       $ctx = $resource->get('context_key');
       $docId = $resource->get('id');
      
       /* set path to default cache file */
       $path = MODX_CORE_PATH . 'cache/resource/' . $ctx . '/resources/' . $docId . '.cache.php';
       unlink($path);
      
      



        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
        • 19328
        • 433 Posts
        Thanks Bob! Unfortunately, both methods, and the combination of both, are not working. Using only the redirect, the manager hangs on save. Using first the clear cache code and afterwards the redirect, the behaviour is the same as it was without your additional code (only working on OnDocFormRender, but only after a manual refresh).

        I think I might have to come up with another approach on this one...
          • 3749
          • 24,544 Posts
          Based on what you've said, I think the parent is actually changed in the DB. One kind of funky option might be to change the parent on the screen with JavaScript injected in your plugin when the conditions merit it.

          Sometimes, though, you just have to tell the client that they won't see the changes unless they reload the page.
            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
            • 19328
            • 433 Posts
            I see what you mean, that could work.

            But you're right, sometimes you've got to find a balance between what might be possible/what the client wishes and what is doable in a reasonable amount of time.