We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36493
    • 64 Posts
    I’m trying to create a plugin that creates a pdf(using wkhtmltopdf) from a resource when its saved (using the system event: OnDocFormSave).

    I need to find out if the resource is a child and if so to make the pdf from the top level resource.

    I thought that getParentIds would give me the info I need but it returns empty when I use it in a plugin (works in a snippet).
    
    $modx->getParentIds($id);
    
    


    Should I be getting an array from getParentIds? or should I iterate over $resource->get(’parent’) until it eq 0?

    trad. revo. 2.0.7 pl
    apache 2.2.3 (CentOS)
    php 5.2.14
    mysql c5.0.90/s5.1.50

      |
      • 3749
      • 24,544 Posts
      It should work fine in a plugin. How are you diagnosing that it’s empty (feel free to post the plugin code)?
        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
        • 36493
        • 64 Posts
        Thanks BobRay!

        ok my plugin:
        <?php
        /**
          * create pdf doc on save from resource
          * 
          * only toplevel resources are used as child resources will be sub items pulled in by getResources 
          *
          */
        
          
          
          ///get proper url to build pdf from
          $url = $modx->makeUrl($id);
          
          $parentIds = implode('-', $modx->getParentIds($id));
          //$parentId = $resource->get('parent');
          
          ///get alias to name pdf file
          $name = $resource->get('alias');
          
          ///build shell command
          $cmd = '/usr/local/bin/wkhtmltopdf -q --margin-bottom 18mm --footer-html http://domain.tld/control/footer.html '.$url.' /var/www/vhosts/domain.tld/httpdocs/control/pdf/'.$name.'-'.$parentIds.'_'.$id.'.pdf';
          
          ///run the command
          shell_exec($cmd);​
        


        Doesn’t do exactly everything I want yet - but the bit that matters re. getParentIds should be output to the filename?

        I have been testing with currently existing resources as I believe that new resources will always return an id of 0
        (Anyone have a workaround for this new docs and missing id situation? Perhaps I need to use a different event?)

          |
          • 3749
          • 24,544 Posts
          If it’s a new document getParentIds() probably won’t work because the document map will not have been updated yet. OnDocFormSave comes before the cache is cleared.

          The makeURL() method may not work either.

          It shouldn’t matter when updating an existing document, but it will when it’s a new doc. You might have to walk up the parent tree yourself using something like this:

          <?php
          /* will tell you if it's a child or not */
          if ($resource->get('parent')) 
          
          /* get the parent object */ 
          parentObj->getOne('Parent');
          
          /* is there a grandparent? */
          ($parentObj->get('parent'))
          
          //etc.
          



          Of course you can use recursion if you prefer.

            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
            • 36493
            • 64 Posts
            Yes the new document before the cache is cleared is a problem but getParentIds still doesn’t work when saving existing documents.

            I did try something like you suggest but got funny results from recursion sad so went for the two stage check as this case is unlikely the have any third level documents.

            new working code:
            <?php
            	/**
            	* create pdf doc on save from resource systemEvent:OnDocFormSave
            	* 
            	* only toplevel resources are used as child resources will be sub items pulled in by getResources 
            	*
            	* does not work for NEW documents!?
            	*
            	*/
            	
            	/**
            	 * check if resource is a top level doc or a child
            	 *	
            	 * @param $res $modx->resource:object
            	 * @return top resourse id:int
            	 */
            	function topparentid($res)
            	{
            		if($res->get('parent') < 1)
            		{
            			return $res->get('id');
            		}
            		else
            		{
            			$parent = $res->getOne('Parent');
            			if($parent->get('parent') < 1)
            			{
            				return $parent->get('id');
            			}
            			else
            			{
            				$grandparent = $parent->getOne('Parent');
            				return  $grandparent->get('id');
            			}
            		}
            	}
            	
            	$resId = topparentid($resource);
            	
            	///get proper url to build pdf from
            	$url = $modx->makeUrl($resId);
            	
            	//$parentIds = implode('-', $modx->getParentIds($id));
            	//$parentId = $resource->get('parent');
            	
            	///get alias to name pdf file
            	$name = $modx->getObject('modResource', $resId)->get('alias');
            	
            	///build shell command
            	//$cmd = '/usr/local/bin/wkhtmltopdf -q --margin-bottom 18mm --footer-html http://domain.tld/control/footer.html '.$url.' /var/www/vhosts/domain.tld/httpdocs/control/pdf/'.$name.'-'.$resId.'_'.$id.'.pdf';
            	
            	$cmd = '/usr/local/bin/wkhtmltopdf -q --margin-bottom 18mm --footer-html http://domain.tld/control/footer.html '.$url.' /var/www/vhosts/domain.tld/httpdocs/control/pdf/'.$name.'.pdf';
            	
            	///run the command
            	shell_exec($cmd);
              |
              • 3749
              • 24,544 Posts
              I can confirm that getParentIds() returns an empty array in a plugin when the same code works in a snippet. It must have something to do with the availability of the resource map. http://bugs.modx.com/issues/10216 [ed. note: BobRay last edited this post 13 years 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
                • 35150 ☆ A M B ☆
                • 191 Posts
                Since you're using it in the mgr context (in a OnDocFormSave-triggered plugin), probably you need to explicitly specify web context:

                $modx->getParentIds($id, 10, array('context' => 'web'));


                There's a little more detail on the RTFM page. [ed. note: jgrant last edited this post 13 years ago.]
                  Extras :: pThumb • Resizer • imageSlim • setPlaceholders
                  • 3749
                  • 24,544 Posts
                  Good catch. Thanks! wink

                    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