We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    getTVValue() only gets the value for that one resource, so you’re not going to get an array back.

    If you’re already looping through the resources, you could add the values to an array as you go by putting this in the loop:


    $categories[] = $resource->getTVValue('category'); 


    But if you’re only executing in OnDocFormSave, you’re just going to get the value of that TV for that resource.

    OTOH, if the TV contains a comma-separated list of categories, you could make an array out of it by doing this:


    $categories = implode(',', $resource->getTVValue('category'));

      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
      • 20196
      • 13 Posts
      I’m almost there this is the plugin code
      <?php
      $eventName = $modx->event->name;
      switch($eventName) 
      {
      	case 'OnDocFormSave':
      		
      		//Referrence to the current Resource tv values
      		$serie = $resource->getTVValue('serie');
      		//$category = $resource->getTVValue('category');
      		$categories = explode(',', $resource->getTVValue('category'));
      		
      		//$parentObject = $modx->getObject('modResource', array('pagetitle' => 'Pool', 'context_key' => $resource->get('context_key') ));
      		
      		//Should ensure that the user is doesn't create symlinks for anything other than resources in the right folder
      		if($resource->get('class_key')!='modDocument' || $resource->get('parent')!= 6/*$parentObject->get('id')*/)
      		{
      			echo 'Symlinks NOT created, REASON: Resource is either not a modDocument or product not created in folder Pool';
      			return;
      		}
      		
      		if(!empty($serie))
      		{
      			$symParentOne = $modx->getObject('modResource',array('pagetitle' => $serie, 'context_key' => $resource->get('context_key') ));
      			$existingObjOne = $modx->getObject('modResource',array('pagetitle'=>$resource->get('pagetitle'),'parent'=>$symParentOne->get('id'),'context_key' => $resource->get('context_key') ));
      			if(!empty($existingObjOne))
      			{
      				return;
      			}
      			else
      			{
      			// Create symlink in serie
      			$fields = array(
      			'template' => 7, /* ID of desired template */
      			'content_type' => 1, /* don't change this */
      			'class_key' => 'modSymLink',
      			'context_key' => $resource->get('context_key'),
      			'parent' => $symParentOne->get('id'),  /* id of desired parent */
      			'richtext' => 1,
      			'pagetitle' => $resource->get('pagetitle'),
      			'alias' => $resource->get('pagetitle'),
      			'hidemenu' => 0,
      			'published' => 1,
      			'searchable' => 0,
      			'cacheable' => 1,
      			'content' => $resource->get('id')   // ID of linked resource
      			);
      			
      			$resource = $modx->newObject('modSymlink', $fields);
      			$resource->save(); 
      			}
      		}
      		if(!empty($categories))
      		{
      			for ( $i = 0; $i <= count($categories); $i++)
      			{
      				$symParentTwo = $modx->getObject('modResource',array('pagetitle' => $categories[$i], 'context_key' => $resource->get('context_key') ));
      				$existingObjTwo = $modx->getObject('modResource',array('pagetitle'=>$resource->get('pagetitle'),'parent'=>$symParentTwo->get('id'),'context_key' => $resource->get('context_key') ));
      				if(!empty($existingObjTwo))
      				{
      					return;
      				}
      				else
      				{
      					$fields = array(
      					'template' => 7, /* ID of desired template */
      					'content_type' => 1, /* don't change this */
      					'class_key' => 'modSymLink',
      					'context_key' => $resource->get('context_key'),
      					'parent' => $symParentTwo->get('id'),  /* id of desired parent */
      					'richtext' => 0,
      					'pagetitle' => $resource->get('pagetitle'),
      					'alias' => $resource->get('pagetitle'),
      					'hidemenu' => 0,
      					'published' => 1,
      					'searchable' => 0,
      					'cacheable' => 1,
      					'content' => $resource->get('id')   // ID of linked resource
      					);
      					
      					$resource = $modx->newObject('modSymlink', $fields);
      					$resource->save();
      				}
      			}
      		}
      		
      		break;
      
      	case 'OnDocFormDelete':
      		
      		//Referrence to the current Resource, get the name of the current resource for removing of symlinks
      		
      		//$parentObject = $modx->getObject('modResource', array('pagetitle' => 'Pool', 'context_key' => $resource->get('context_key') ));
      		//Should ensure that the user is doesn't delete symlinks for anything other than resources in the right folder
      		if($resource->get('class_key')!='modDocument'|| $resource->get('parent')!= 6/*$parentObject->get('id')*/)
      		{
      			return;
      		}
      
      		$symlinks = $modx->getCollection('modSymlink',array('pagetitle'=>$resource->get('pagetitle')));
      		if(!empty($symlinks))
      		{
      			$modx->removeCollection('modSymlink',array('pagetitle'=>$resource->get('pagetitle')));
      		}
      		
      		break;
      }
      


      It adds the needed symlinks like it is suppose to, but the manager hangs, so a refresh is needed. Anyone who can offer any advice to eliminate this final obstacle.

      Best Regards
      Moonkin
        • 3749
        • 24,544 Posts
        Try making sure $symParentOne is not null before calling its get() method.

        Also, comment out the ’echo’ statement. If you return the value in the echo statement rather than echoing it, it should go in the error log, or you can use the method below.

        Here’s a method I invented for debugging plugins: Create a chunk called debug. Then use this code for any debugging info:

        $chunk = $modx->getObject('modChunk', array('name'=>'debug'));
        
        /* Put any debugging information in the string, $s */
        $ids = $modx->getParentIds(58); /* example function */
        $s = 'Debug output' . "\n\n";
        $s .= print_r($ids,true); /* example debug output */
        
        $chunk->setContent($s);
        $chunk->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
          • 20196
          • 13 Posts
          That did it!!!

          Thanks a billion

          Best Regards
          Moonkin
            • 20196
            • 13 Posts
            One last thing

            This thread http://modxcms.com/forums/index.php/topic,59415.0.html points out that, by creating a resource and corresponding symlinks, getResources won’t be able to look at the symlinks Tvs and filter by them.

            This kinda breaks up my plugin idea, but there is a way, isn’t there wink

            What if I at the creation of the symlink also copied the TVs, is it possible, and how would I go about.

            Best regards
            Moonkin
              • 3749
              • 24,544 Posts
              A post in another topic by OpenGeek hinted that getResources may get the TVs of the target if they aren’t filled in on the Symlink, but I haven’t tried it.

              [Update] This has been clarified and apparently that’s not possible.

              Try creating the matching TVs manually for the Symlinks and see if getResources handles them the way you want. If so, you could definitely set them when creating the Symlink.

              Basically, that would look something like this (plus some sanity checking):

              <?php
              /* Do this after creating the Symlink, saving it, and getting its ID. */
              
              /* $resource is the resource object you're creating the Symlink for. */
              /* $symlinkId is the ID of  the Symlink you've just created. */
              
              /* Get the ID of the resource (if you don't have it already) */
              $resourceId = $resource->get('id');
              
              /* get the TV's value for that resource */
              $tv = $modx->getObject('modTemplateVar', array( 'name' => 'MyTv'));
              $tvId = $tv->get('id');
              $tvValue = $tv->getValue($resourceId);
              
              /* create the new templateVarResource object for the symlink
                  to hold the duplicate value of the TV from the resource 
              */
              
              $fields = array();
              $fields['tmplvarid'] = $tvId;
              $fields['contentid'] = $symlinkId;
              $tvt = $modx->newObject('modTemplateVarResource', $fields);
              
              /* duplicate the value from the resource in the Symlink */
              $tvt->set('value', $tvValue);
              $tvt->save();
              


              If you know the ID of the TV, it would be a little faster to do it like this:

              <?php
              
              $tvId = 8;
              $resourceId = $resource->get('id');
              $tv = $modx->getObject('modTemplateVar', $tvId);
              
              
              $tvValue = $tv->getValue($resourceId);
              
              $fields = array();
              $fields['tmplvarid'] = $tvId;
              $fields['contentid'] = $symlinkId;
              $tvt = $modx->newObject('modTemplateVarResource', $fields);
              
              $tvt->set('value', $tvValue);
              $tvt->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
                • 20196
                • 13 Posts
                Will do, thanks smiley

                Best regards
                Moonkin
                  • 20196
                  • 13 Posts
                  Okay i feel I’m so close i can taste it.

                  I’ve created a for loop of TVs to be added really only need two of them right now, but I thought that might as well add the others.

                  The manager hangs while trying to test it and the TVs aren’t copied, but I suspect that it is because $symId isn’t defined or maybe even my $tvArray anyways here’s the code as far;

                  <?php
                  $eventName = $modx->event->name;
                  switch($eventName) 
                  {
                  	case 'OnDocFormSave':
                  		
                  		//Referrence to the current Resource tv values
                  		$idx = $resource->get('id');
                  		$serie = $resource->getTVValue('serie');
                  		//$category = $resource->getTVValue('category');
                  		$categories = explode(',', $resource->getTVValue('category'));
                  		
                  		//$parentObject = $modx->getObject('modResource', array('pagetitle' => 'Pool', 'context_key' => $resource->get('context_key') ));
                  		
                  		//Should ensure that the user is doesn't create symlinks for anything other than resources in the right folder
                  		if($resource->get('class_key')!='modDocument' || $resource->get('parent')!= 6/*$parentObject->get('id')*/)
                  		{
                  			return;
                  		}
                  		
                  		if(!empty($serie))
                  		{
                  			$symParentOne = $modx->getObject('modResource',array('pagetitle' => $serie, 'context_key' => $resource->get('context_key') ));
                  			if(empty($symParentOne))
                  			{
                  				return;
                  			}
                  			else
                  			{
                  				$existingObjOne = $modx->getObject('modResource',array('pagetitle'=>$resource->get('pagetitle'),'parent'=>$symParentOne->get('id'),'context_key' => $resource->get('context_key') ));
                  				if(!empty($existingObjOne))
                  				{
                  					return;
                  				}
                  				else
                  				{
                  				// Create symlink in serie
                  				$fields = array(
                  				'template' => 7, /* ID of desired template */
                  				'content_type' => 1, /* don't change this */
                  				'class_key' => 'modSymLink',
                  				'context_key' => $resource->get('context_key'),
                  				'parent' => $symParentOne->get('id'),  /* id of desired parent */
                  				'richtext' => 1,
                  				'pagetitle' => $resource->get('pagetitle'),
                  				'alias' => $resource->get('pagetitle'),
                  				'hidemenu' => 0,
                  				'published' => 1,
                  				'searchable' => 0,
                  				'cacheable' => 1,
                  				'content' => $idx   // ID of linked resource
                  				);
                  				
                  				$resource = $modx->newObject('modSymlink', $fields);
                  				$resource->save();
                  				
                  				//obtain the id of the symlink just created 
                  				$symId = $resource->get('id');
                  				
                  				}
                  			}
                  		}
                  		if(!empty($categories))
                  		{
                  			for ( $i = 0; $i <= count($categories); $i++)
                  			{
                  				$symParentTwo = $modx->getObject('modResource',array('pagetitle' => $categories[$i], 'context_key' => $resource->get('context_key') ));
                  				if(empty($symParentTwo))
                  				{
                  					return;
                  				}
                  				else
                  				{
                  					$existingObjTwo = $modx->getObject('modResource',array('pagetitle'=>$resource->get('pagetitle'),'parent'=>$symParentTwo->get('id'),'context_key' => $resource->get('context_key') ));
                  					if(!empty($existingObjTwo))
                  					{
                  						return;
                  					}
                  					else
                  					{
                  						$fields = array(
                  						'template' => 7, /* ID of desired template */
                  						'content_type' => 1, /* don't change this */
                  						'class_key' => 'modSymLink',
                  						'context_key' => $resource->get('context_key'),
                  						'parent' => $symParentTwo->get('id'),  /* id of desired parent */
                  						'richtext' => 0,
                  						'pagetitle' => $resource->get('pagetitle'),
                  						'alias' => $resource->get('pagetitle'),
                  						'hidemenu' => 0,
                  						'published' => 1,
                  						'searchable' => 0,
                  						'cacheable' => 1,
                  						'content' => $idx   // ID of linked resource
                  						);
                  						
                  						$resource = $modx->newObject('modSymlink', $fields);
                  						$resource->save();
                  						
                  					}
                  				}
                  			}
                  		}
                  		
                  		//Fill in symLinks TVs, basically all of them, but only for the symlink within serie
                  		$tvArray = array(3,4,5,6,7,39); // The TV ids I want to dublicate
                  		
                  		for ($j=0; $j<=count($tvArray); $j++)
                  		{
                  			if(empty($symId))
                  			{
                  				return;
                  			}
                  			else
                  			{
                  				//locate the template variable from the array
                  				$tv = $modx->getObject('modTemplateVar', $tvArray[$j]);
                  				//now get the value of the template variable from our main resource
                  				$tvValue = $tv->getValue($idx);
                  				//continue as long as the value isn't empty
                  				if(!empty($tvValue))
                  				{
                  					//setup the criteria for the new tempate variable
                  					$fields = array();
                  					
                  					$fields['tmplvarid'] = $tvArray[$j];
                  					$fields['contentid'] = $symId;
                  					$fields['value'] = $tvValue;
                  					$tvt = $modx->newObject('modTemplateVarResource', $fields);
                  					$tvt->save();
                  				}
                  				else
                  				{
                  					return;	
                  				}
                  			}
                  		}
                  		
                  		
                  		break;
                  
                  	case 'OnDocFormDelete':
                  		
                  		//Referrence to the current Resource, get the name of the current resource for removing of symlinks
                  		
                  		//$parentObject = $modx->getObject('modResource', array('pagetitle' => 'Pool', 'context_key' => $resource->get('context_key') ));
                  		//Should ensure that the user is doesn't delete symlinks for anything other than resources in the right folder
                  		if($resource->get('class_key')!='modDocument'|| $resource->get('parent')!= 6/*$parentObject->get('id')*/)
                  		{
                  			return;
                  		}
                  
                  		$symlinks = $modx->getCollection('modSymlink',array('pagetitle'=>$resource->get('pagetitle')));
                  		if(!empty($symlinks))
                  		{
                  			$modx->removeCollection('modSymlink',array('pagetitle'=>$resource->get('pagetitle')));
                  		}
                  		
                  		break;
                  }
                  


                  In addition I’ve moved $tvt->set(’value’, $tvValue); inside the fields array, but IMO that shouldn’t be the reason for the hang.

                  Anyways I’m looking forward to hearing from you guys wink

                  Best regards
                  Moonkin
                    • 3749
                    • 24,544 Posts
                    I don’t see anything wrong. You should probably add some more sanity checks.

                    newObject() returns null on failure, save() returns false on failure.

                    You might also convert the OnDocFormSave part to a snippet that takes the resource ID as a property and gets $resource with getObject(). That should let you see the error message when it hangs.
                      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
                      • 47777
                      • 1 Posts
                      Quote from: moonkin at Aug 09, 2011, 05:20 AM
                      Okay i feel I'm so close i can taste it.

                      I've created a for loop of TVs to be added really only need two of them right now, but I thought that might as well add the others.

                      The manager hangs while trying to test it and the TVs aren't copied, but I suspect that it is because $symId isn't defined or maybe even my $tvArray anyways here's the code as far;

                      <!--?php
                      $eventName = $modx--->event->name;
                      switch($eventName) 
                      {
                      	case 'OnDocFormSave':
                      		
                      		//Referrence to the current Resource tv values
                      		$idx = $resource->get('id');
                      		$serie = $resource->getTVValue('serie');
                      		//$category = $resource->getTVValue('category');
                      		$categories = explode(',', $resource->getTVValue('category'));
                      		
                      		//$parentObject = $modx->getObject('modResource', array('pagetitle' => 'Pool', 'context_key' => $resource->get('context_key') ));
                      		
                      		//Should ensure that the user is doesn't create symlinks for anything other than resources in the right folder
                      		if($resource->get('class_key')!='modDocument' || $resource->get('parent')!= 6/*$parentObject->get('id')*/)
                      		{
                      			return;
                      		}
                      		
                      		if(!empty($serie))
                      		{
                      			$symParentOne = $modx->getObject('modResource',array('pagetitle' => $serie, 'context_key' => $resource->get('context_key') ));
                      			if(empty($symParentOne))
                      			{
                      				return;
                      			}
                      			else
                      			{
                      				$existingObjOne = $modx->getObject('modResource',array('pagetitle'=>$resource->get('pagetitle'),'parent'=>$symParentOne->get('id'),'context_key' => $resource->get('context_key') ));
                      				if(!empty($existingObjOne))
                      				{
                      					return;
                      				}
                      				else
                      				{
                      				// Create symlink in serie
                      				$fields = array(
                      				'template' => 7, /* ID of desired template */
                      				'content_type' => 1, /* don't change this */
                      				'class_key' => 'modSymLink',
                      				'context_key' => $resource->get('context_key'),
                      				'parent' => $symParentOne->get('id'),  /* id of desired parent */
                      				'richtext' => 1,
                      				'pagetitle' => $resource->get('pagetitle'),
                      				'alias' => $resource->get('pagetitle'),
                      				'hidemenu' => 0,
                      				'published' => 1,
                      				'searchable' => 0,
                      				'cacheable' => 1,
                      				'content' => $idx   // ID of linked resource
                      				);
                      				
                      				$resource = $modx->newObject('modSymlink', $fields);
                      				$resource->save();
                      				
                      				//obtain the id of the symlink just created 
                      				$symId = $resource->get('id');
                      				
                      				}
                      			}
                      		}
                      		if(!empty($categories))
                      		{
                      			for ( $i = 0; $i <= count($categories); $i++)
                      			{
                      				$symParentTwo = $modx->getObject('modResource',array('pagetitle' => $categories[$i], 'context_key' => $resource->get('context_key') ));
                      				if(empty($symParentTwo))
                      				{
                      					return;
                      				}
                      				else
                      				{
                      					$existingObjTwo = $modx->getObject('modResource',array('pagetitle'=>$resource->get('pagetitle'),'parent'=>$symParentTwo->get('id'),'context_key' => $resource->get('context_key') ));
                      					if(!empty($existingObjTwo))
                      					{
                      						return;
                      					}
                      					else
                      					{
                      						$fields = array(
                      						'template' => 7, /* ID of desired template */
                      						'content_type' => 1, /* don't change this */
                      						'class_key' => 'modSymLink',
                      						'context_key' => $resource->get('context_key'),
                      						'parent' => $symParentTwo->get('id'),  /* id of desired parent */
                      						'richtext' => 0,
                      						'pagetitle' => $resource->get('pagetitle'),
                      						'alias' => $resource->get('pagetitle'),
                      						'hidemenu' => 0,
                      						'published' => 1,
                      						'searchable' => 0,
                      						'cacheable' => 1,
                      						'content' => $idx   // ID of linked resource
                      						);
                      						
                      						$resource = $modx->newObject('modSymlink', $fields);
                      						$resource->save();
                      						
                      					}
                      				}
                      			}
                      		}
                      		
                      		//Fill in symLinks TVs, basically all of them, but only for the symlink within serie
                      		$tvArray = array(3,4,5,6,7,39); // The TV ids I want to dublicate
                      		
                      		for ($j=0; $j<=count($tvArray); $j++)
                      		{
                      			if(empty($symId))
                      			{
                      				return;
                      			}
                      			else
                      			{
                      				//locate the template variable from the array
                      				$tv = $modx->getObject('modTemplateVar', $tvArray[$j]);
                      				//now get the value of the template variable from our main resource
                      				$tvValue = $tv->getValue($idx);
                      				//continue as long as the value isn't empty
                      				if(!empty($tvValue))
                      				{
                      					//setup the criteria for the new tempate variable
                      					$fields = array();
                      					
                      					$fields['tmplvarid'] = $tvArray[$j];
                      					$fields['contentid'] = $symId;
                      					$fields['value'] = $tvValue;
                      					$tvt = $modx->newObject('modTemplateVarResource', $fields);
                      					$tvt->save();
                      				}
                      				else
                      				{
                      					return;	
                      				}
                      			}
                      		}
                      		
                      		
                      		break;
                      
                      	case 'OnDocFormDelete':
                      		
                      		//Referrence to the current Resource, get the name of the current resource for removing of symlinks
                      		
                      		//$parentObject = $modx->getObject('modResource', array('pagetitle' => 'Pool', 'context_key' => $resource->get('context_key') ));
                      		//Should ensure that the user is doesn't delete symlinks for anything other than resources in the right folder
                      		if($resource->get('class_key')!='modDocument'|| $resource->get('parent')!= 6/*$parentObject->get('id')*/)
                      		{
                      			return;
                      		}
                      
                      		$symlinks = $modx->getCollection('modSymlink',array('pagetitle'=>$resource->get('pagetitle')));
                      		if(!empty($symlinks))
                      		{
                      			$modx->removeCollection('modSymlink',array('pagetitle'=>$resource->get('pagetitle')));
                      		}
                      		
                      		break;
                      }
                      


                      In addition I've moved $tvt->set('value', $tvValue); inside the fields array, but IMO that shouldn't be the reason for the hang.

                      Anyways I'm looking forward to hearing from you guys wink

                      Best regards
                      Moonkin

                      Hi Moonkin,

                      I found your solution, but not clearly understand how to use it? Where I need to post this code and do I need to change anything in it?

                      In resources tab I have container "test1" where I create documents (resources) and I need to have automatically created symlink (actually two of even three symlinks) in container "test2" (and "test3" and "test4") with link to this created document (resource)

                      As result I will have few links:

                      link .../test1/newdocument.html
                      link .../test2/newdocument.html
                      link .../test3/newdocument.html
                      link .../test4/newdocument.html