We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 51192
    • 11 Posts
    Hey there

    I've trying to create a new plugin for my needs: if some documents will be created or updated (OnBeforeDocFormSave), it should create a new document with the nearly the same values as current document. It doesnt work like expected and my code is a bit ugly, maybe someone can help me a little bit.

    First thing is, i have to read the ultimate parent from the current document. My tree looks like this:


    In this sample the current document has the id 25. The plugin should only make anything if the current document is a child of document id 2 and if its two levels deeper. To get the id of the parent parent node, i use this:
    $parentId = $resource->parent;
    
    if ($parentId) {
        $parentResource = $modx->getObject('modResource',$parentId);
    
        if ($parentResource) {
            $parentId1 = $parentResource->parent;
    
            if ($parentId1) {
                $parentId = $parentId1;
            }
        }
    }

    It works, but it's ugly and i am shure it can be done better. I've tried to use the snipped UltimateParent with runSnippet, but it doesn't work in this case, don't know why. I've tried this solution too, but doesn't work in my case: https://www.sepiariver.ca/blog/modx-web/modx-quick-tip-get-id-of-grandparent/

    And i didn't check the two levels, i currently don't have an idea how to do that.

    Now i am creating a new resource with some values, if the document is not already existing with this pagetitle:
        switch ($modx->event->name) {
            case 'OnBeforeDocFormSave':
                $document = $modx->getObject('modResource',array(
                    'pagetitle' => $resource->pagetitle,
                    'parent' => 3
                ));
    
                if (!$document)
                {
                    $resource = $modx->newObject('modResource');
                    $resource->fromArray(array(
                        'pagetitle' => $resource->pagetitle,
                        'parent' => 3,
                        'alias' => $resource->alias,
                        'published' => 1,
                        'hidemenu' => 1,
                        'template' => 6
                    ));
    
                    $resource->save();
                }
    
                break;
        }

    This works almost, but some values are not passed to the new document (pagetitle, alias). What's wrong with it?

    This question has been answered by BobRay. See the first response.

    [ed. note: tom331 last edited this post 7 years, 11 months ago.]
    • discuss.answer
      • 3749
      • 24,544 Posts
      I think I would do it this way instead rather than using newObject():

      $fields = array(
          'pagetitle' => $resource->get('pagetitle'),
          'parent' => 3,
          'alias' => $resource->get('alias'),
          'published' => 1,
          'hidemenu' => 1,
          'template' => 6,
      );
      
      $modx->runProcessor('resource/create', $fields);


      BTW, do you really want to be creating a new resource with exactly the same pagetitle and alias as the old one? That may be why it's failing. [ed. note: BobRay last edited this post 7 years, 11 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
        • 51192
        • 11 Posts
        Hi Bob, I know it sounds really strange but i need to have a document with exactly the same pagetitle and alias in another document node, the uri differs.

        With runProcessor and $resource->get it works like a charm! Thank you very much for your suggestion! I've also reworked part 1 of my code to find the parent parent of the current resource, but I think it can be done much better - but it works for now.

        I've added this after runProcessor to get error messages:
                        if ($response->isError()) {
                            return $response->getMessage();
                        }
          • 3749
          • 24,544 Posts
          There are bunch of ways to get the grandparent's ID. This is the most straightforward:

          $parentObj = $modx->getObject('modResource', $modx->resource->get('parent'));
          $grandparentId = $parentObj? (int) $parentObj->get('id') : 0;


          I think this would be significantly faster but I'm not positive that I have it right:

          $query = $modx->newQuery('modResource', array(
              'id' => $resource->get('parent'),
          ));
          $query->select('parent');
          $grandparentId = $modx->getValue($query->prepare());


          It's faster because it never has to call getObject() to retrieve the entire grandparent object. It just gets its parent field.



            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
            • 51192
            • 11 Posts
            Late response, but...the second one works without flaws. Thanks!