We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40098
    • 9 Posts
    Hi!
    Long time lurker, first time poster. I've been able to pretty much answer 99% of my modx questions by just searching the forum but I've come to a bit of stop with the question in the title.
    I'm a junior web developer and I'm still getting my head around how modx works, so if you think I'm doing something wrong, than please tell me!

    This is my snippet call
    [[!getOptions? &parents=`232` &name=`ScholarCollege` &"noEmptySchools" => $noEmptySchools=`1`]] 


    (I've missed a few steps, basically it's fed through another snippet and chunk which creates the <select> <option> nothing really important)

    and this is my snippet getOptions
    <?php
    //Set resource ids to include
    $resources = !empty($resources) ? $resources : false;
    //Set parent resources to search within
    $parents = !empty($parents) ? $parents : false;
    //Set whether or not to include resources that are containers
    $includeContainers = !empty($includeContainers) ? $includeContainers : 0;
    //Set whether or not to include resources that have been deleted
    $includeDeleted = !empty($includeDeleted) ? $includeDeleted : 0;
    //Set whether or not to include resources that have been deleted
    $options = !empty($options) ? $options : false;
    
    if(!$options) {
    	//Create object for resource
    	$query = $modx->newQuery('modResource');
    	//Get children of resource
    	if($parents) {
    		$parentsArray = explode(',',$parents);
    		$query->where(array('OR:modResource.parent:IN' => $parentsArray), xPDOQuery::SQL_OR);
    	}
    	//Get resources
    	if($resources) {
    		$resourcesArray = explode(',',$resources);
    		$query->where(array('OR:modResource.id:IN' => $resourcesArray), xPDOQuery::SQL_OR);
    	}
    	//build remaining conditions
    	$query->where(array(
    	   //'parent' => $parents,
    	   'isfolder' => $includeContainers,
    	   'deleted' => $includeDeleted,
    	));
    	//sort by page title
    	$query->sortby('pagetitle','ASC');
    	$options = $modx->getCollection('modResource',$query);
    }
    
    $optionList = '';
    foreach ($options as $item):
    
    	if($excludeEmptyParents):
    		if($item->hasChildren('id')):
    			$optionList .= $item->get('pagetitle') . "==" . $item->get('id') . "||";
    		endif;
    	else:
    		$optionList .= $item->get('pagetitle') . "==" . $item->get('id') . "||";
    	endif;
    
    	#if($noEmptySchools):
    		#$optionList .= $item->getTVValue($item->get('id')). "==" . $item->getTVValue('id') . "||";
    	#endif;	
    
    
    endforeach;
    $optionList = substr($optionList, 0, -2);
    return $optionList;



    Basically, the code is pretty straight forward, I'm building a list of resources from a given parent ID and then fed them into a select item, the resourceid is the value of the select item and the page name is the title of the select item.

    I'm using the snippet for a few things (hence why there's "excludeEmptyParents", you can ignore that).

    The part I'm interested in is the lines 48-50.

    I want to check if the ID of the resource in the select item, exists as a value in the template variable table, if a template variable with the value of that resource ID exists then I want to show it in the drop down boxes, if it doesn't exist then I don't want it to be visible.

    What would be the best approach to do such a task? I have searched and I haven't anything which matches what I'm trying to achieve (Although maybe I have and I'm just to blind to see).

    If anyway has any input on the subject I would be forever grateful!
    Thanks!
    Kingsley


      • 3749
      • 24,544 Posts
      The argument to $resource->getTVValue() is the name or ID of the TV, not the resource. wink


      Don't feel bad -- it's often confused with $tv->getValue($resourceId).


      You could also check for the existence of a modTemplateVarResource object:

      $tvr = $modx->getObject('modTemplateVarResource', array (
          'tmplvarid' => $tvId, 
          'contentid' => $resourceId
      ));
      if (! tvr) {
         /* not in the table */
      }
      


      Be aware, though that TVs for a resource that are set to the TV's default value, are not stored as TVTs, so those would appear empty.

      What you have above is probably the best method (once you fix it) since you already have each resource.



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 40098
        • 9 Posts
        Awh man! So close, yet so far. Thanks for that!

        Yeah, I learnt the hard way with the default TV value not getting stored, 140 resources later I realized what I had done wrong.

        Do you happen to have a working version of $resource->getTVValue() in the context i'm trying to use it in?

        Would this work?
        $tvr = $modx->getObject('modTemplateVarResource', array (
            'tmplvarid' => $tvId,
            'value' => $resourceId
        ));
        if (! tvr) {
           /* not in the table */
        }
          • 3749
          • 24,544 Posts
          It would work for TVs not set to the default value, but I think what you really want is to just change line 49 to use this:

          $item->getTVValue($tvId);


          instead of this:

          $item->getTVValue($item->get('id'))




          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            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
            • 40098
            • 9 Posts
            Hey man, thanks for the reply!

            How's this?
            <?php
            //Set resource ids to include
            $resources = !empty($resources) ? $resources : false;
            
            //Set parent resources to search within
            $parents = !empty($parents) ? $parents : false;
            
            //Set whether or not to include resources that are containers
            $includeContainers = !empty($includeContainers) ? $includeContainers : 0;
            
            //Set whether or not to include resources that have been deleted
            $includeDeleted = !empty($includeDeleted) ? $includeDeleted : 0;
            
            //Set whether or not to include resources that have been deleted
            $options = !empty($options) ? $options : false;
            
            if(!$options) {
            	//Create object for resource
            	$query = $modx->newQuery('modResource');
            	//Get children of resource
            	if($parents) {
            		$parentsArray = explode(',',$parents);
            		$query->where(array('OR:modResource.parent:IN' => $parentsArray), xPDOQuery::SQL_OR);
            	}
            	//Get resources
            	if($resources) {
            		$resourcesArray = explode(',',$resources);
            		$query->where(array('OR:modResource.id:IN' => $resourcesArray), xPDOQuery::SQL_OR);
            	}
            	//build remaining conditions
            	$query->where(array(
            	   //'parent' => $parents,
            	   'isfolder' => $includeContainers,
            	   'deleted' => $includeDeleted,
            	));
            	//sort by page title
            	$query->sortby('pagetitle','ASC');
            	$options = $modx->getCollection('modResource',$query);
            }
            
            $optionList = '';
            foreach ($options as $item):
            
            	if($excludeEmptyParents):
            		if($item->hasChildren('id')):
            			$optionList .= $item->get('pagetitle') . "==" . $item->get('id') . "||";
            		endif;
            	else:
            		
            		if($noEmptySchools):
            			$tvr = $modx->getObject('modTemplateVarResource', array(
            				'tmplvarid' => '65',
            				'value' => $item->get('id')
            			));
            			if ($tvr): //if does appear in tv resource table
            				$optionList .= $item->get('pagetitle') . "==" . $item->get('id') . "||";		
            			endif;
            		else:
            			$optionList .= $item->get('pagetitle') . "==" . $item->get('id') . "||";
            		endif;	
            	endif;
            
            endforeach;
            $optionList = substr($optionList, 0, -2);
            return $optionList;
            


            How does that look? Can you see any improvements?
            Cheers buddy!
              • 3749
              • 24,544 Posts
              Assuming that those variables ($resources, $includeContainers, etc.) are coming in as properties of the snippet, it's a little more proper to get them from the $scriptProperties array.

              So instead of:

              $resources = !empty($resources) ? $resources : false;


              you'd have:

              $resources = !empty($scriptProperties['resources']) ? $scriptProperties['resources'] : false;


              For less typing, you can also do something like this:


              $sp =& $scriptProperties;
              $resources = !empty($sp['resources']) ? $sp['resources'] : false;




              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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