We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    As the subject says, how can I get all the TV variable names and values for a specific resource in a snippet?

    Is there a handy method for that or do I have to get a list of available TV variables and iterate over it looking to see if the resource has any? i.e.

    $tvs = array('tvone','tvtwo','tvthree... etc);
    
    foreach($tvs as $tv){
    
        $resource= $modx->getObject('modResource', 123);
    
        $tvValue = $resource->getTVValue($tv);
    
        if($tvValue){
    
             // do something
    
        }
    
    }



    Something like that seems cumbersome


      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 3749
      • 24,544 Posts
      This page is your friend: http://bobsguides.com/modx-object-quick-reference.html#modResource wink
      (scroll down to the aliases)

      <?php
      $output = '';
      $resource= $modx->getObject('modResource', 123);
      $tvs = $resource->getMany('TemplateVars')
      
      
      if (!empty($tvs)) {
         foreach($tvs as $tv) {
            $name = $tv->get('name');
            $value = $tv->renderOutput(123);
            $output .= '<br /><br />Name: ' . $name . <br /> . 'Value: ' . $value;
         }
      } else {
          $output = 'No TVs found');
      }
      
      return $output;


      That should return the default value if the TV has no value.
      Note that it will get the processed values of the TVs. If you can use the raw values (e.g no image or @ binding TVs), use:

      $value = $tv->getValue(123);


      It will be slightly faster.
        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
        • 26503
        • 620 Posts
        Thanks Bob, as always, 'modx has an easier way to do it' ... it would up in a function like this:

        /*
        *  getBranch
        *  return one one level of one branch by single id 
        */
        function getBranch($id){
        	
        	$children = $this->modx->getChildIds($id, 1);
        	
        	$children[] = $id;
        	
        	$branch = array();
        	
        	foreach($children as $id){
        	
        		$resource = $this->modx->getObject('modResource',$id);
        		
        		$templateVars = array();
        
        		$content = array();
        			
        		if($resource){
        			
        			$content = array(
        				'id' => $resource->get('id'),
        				'type' => $resource->get('type'),
        				'contentType' => $resource->get('contentType'),
        				'pagetitle' => $resource->get('pagetitle'),
        				'longtitle' => $resource->get('longtitle'),
        				'description' => $resource->get('description'),
        				'alias' => $resource->get('alias'),
        				'llink_attributes' => $resource->get('llink_attributes'),
        				'published' => $resource->get('published'),
        				'pub_date' => $resource->get('pub_date'),
        				'unpub_date' => $resource->get('unpub_date'),
        				'parent' => $resource->get('parent'),
        				'isfolder' => $resource->get('isfolder'),
        				'introtext' => $resource->get('introtext'),
        				'content' => $resource->get('content'),
        				'richtext' => $resource->get('richtext'),
        				'template' => $resource->get('template'),
        				'menuindex' => $resource->get('menuindex'),
        				'searchable' => $resource->get('searchable'),
        				'cacheable' => $resource->get('cacheable'),
        				'createdby' => $resource->get('createdby'),
        				'createdon' => $resource->get('createdon'),
        				'editedby' => $resource->get('editedby'),
        				'editedon' => $resource->get('editedon'),
        				'deleted' => $resource->get('deleted'),
        				'deletedon' => $resource->get('deletedon'),
        				'deletedby' => $resource->get('deletedby'),
        				'publishedon' => $resource->get('publishedon'),
        				'publishedby' => $resource->get('publishedby'),
        				'menutitle' => $resource->get('menutitle'),
        				'donthit' => $resource->get('donthit'),
        				'privateweb' => $resource->get('privateweb'),
        				'privatemgr' => $resource->get('privatemgr'),
        				'content_dispo' => $resource->get('content_dispo'),
        				'hidemenu' => $resource->get('hidemenu'),
        				'class_key' => $resource->get('class_key'),
        				'context_key' => $resource->get('context_key'),
        				'content_type' => $resource->get('content_type'),
        				'uri' => $resource->get('uri'),
        				'uri_override' => $resource->get('uri_override'),
        				'hide_children_in_tree' => $resource->get('hide_children_in_tree'),
        				'show_in_tree' => $resource->get('show_in_tree'),
        				'properties' => $resource->get('properties')
        				);	
        				
        				$tvs = $resource->getMany('TemplateVars');
        		 
        				if (!empty($tvs)) {
        					
        					foreach($tvs as $tv) {
        						
        						$name = $tv->get('name');
        						$value = $tv->getValue($id);
        						// if we need the value rendered:
        						// $value = $tv->renderOutput($id);
        						$templateVars[] = array($name => $value);
        						
        				}
        				
        			}
        				
        		}
        				
        		$content['template_variables'] = $templateVars;	
        		
        		$branch[$id] = $content;
        	
        	}
        	
        	return $branch;
        
        }
        


        is there a faster/easier way to generate that $content array?? [$modx->justGimmeTheContent->($resource); smiley ??]
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 26503
          • 620 Posts
          Never mind - I got it:

          /*
          	*  getBranch
          	*  return one one level of one branch by single id 
          	*/
          	function getBranch($id){
          		
          		$children = $this->modx->getChildIds($id, 1);
          		
          		$children[] = $id;
          		
          		$branch = array();
          		
          		foreach($children as $id){
          		
          			$resource = $this->modx->getObject('modResource',$id);
          			
          			$templateVars = array();
          	
          			$content = array();
          				
          			if($resource){
          				
          				$fields = $this->modx->getFields('modResource');
          			
          				foreach($fields as $key => $value){
          					
          					$content[$key] = $resource->get($key);
          					
          				}
          					
          					$tvs = $resource->getMany('TemplateVars');
          			 
          					if (!empty($tvs)) {
          						
          						foreach($tvs as $tv) {
          							
          							$name = $tv->get('name');
          							$value = $tv->getValue($id);
          							// if we need the value rendered:
          							// $value = $tv->renderOutput($id);
          							$templateVars[] = array($name => $value);
          							
          					}
          					
          				}
          					
          			}
          					
          			$content['template_variables'] = $templateVars;	
          			
          			$branch[$id] = $content;
          		
          		}
          		
          		return $branch;
          	
          	}
          
            *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

            Sean Kimball CLP, CLS.
            Technical Director / Sr. Developer | BigBlock Studios
            ._______________________________________________.
            Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
            27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
            phone/fax: 905-426-5525
            • 3749
            • 24,544 Posts
            LOL. Speaking of MODX having an easier way, try replacing this:

            $fields = $this->modx->getFields('modResource');
                         
            foreach($fields as $key => $value){
             
                $content[$key] = $resource->get($key);
                               
            }
            
            $fields  = $resource->toArray();
            

            with this:

            $content = $resource->toArray();
              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
              • 26503
              • 620 Posts
              Quote from: BobRay at Feb 23, 2013, 09:43 PM


              $content = $resource->toArray();

              hahaha.... I gotta stop drinking on the weekends...

              thanks Bob.
                *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                Sean Kimball CLP, CLS.
                Technical Director / Sr. Developer | BigBlock Studios
                ._______________________________________________.
                Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                phone/fax: 905-426-5525
                • 3749
                • 24,544 Posts
                Isn't that what weekends are for? smiley
                  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