We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38359
    • 93 Posts
    Hi there,

    I'm trying to create a search facility that returns a list of page (via getResources) based on the value of the page's template variables.
    For example the search form will have a dropdown for Region, the user selects a region, submits the form and is shown all documents where the template variable 'Region' is the same as the value selected.

    So I'm trying to write a snippet which returns a list of page ids based on the value of a template variable, can anyone point me in the right direction?

    I know how to get the value of the TV for a given doc id (Thanks to Bob!)

    $tvId = 1; //the tv id I want to get
    $tv = $modx->getObject('modTemplateVar',$tvId);
    $tvContent = $tv->getValue($pageId);
    return $tvContent;


    So I guess I just need to iterate through all the docs and find the ones where the tv is set to the required value?
    Or is there an easier way to do it?


    Thanks in advance.
    [ed. note: wilbobaggins last edited this post 13 years, 6 months ago.]
      • 36924 ☆ A M B ☆
      • 298 Posts
      I do not think you need to write a snippet for this. SimpleSearch can handle this, you just convert the text box to a dropdown.
        • 38359
        • 93 Posts
        Thanks ojchris,
        I looked at SimpleSearch and it says that it can't yet search by template variable. There will be several fields that the user can search by if that makes a difference?
          • 36996
          • 211 Posts
          You can do that with AdvSearch - http://rtfm.modx.com/display/ADDON/AdvSearch
          There are good tutorials here - http://www.revo.wangba.fr/faceted-search-1.html
            • 38359
            • 93 Posts
            Thanks nir-z, I'll check those out and let you know how it goes
              • 40045
              • 534 Posts
              How about something like this (see comments for details)

              <?php
              $query = $_POST['region']; // The value submitted by the selectbox/form
              $resources = array(); // all resource ids that have the requested value
              $tvresources = $modx->getCollection('modTemplateVarResource', array('tmplvarid' => 36)); // find all resouces that have a value in the tv with id = 36
              foreach ($tvresources as $tvresource) {
              	$tvval = $tvresource->get('value'); // get the value of the tv, needed for the following check
              	if ( $tvval === $query ) ) {
              		// if the requested value is the same as the value of that tv, add it to the array
              		$resources[] += $tvresource->get('contentid');
              	}
              }
              // return a comma separated list of resource ids for getResources
              return implode(',' $resources);
              
                • 38359
                • 93 Posts
                Thanks exside, that's brilliant. I'm going to check out your solution and nir-z's. I'll let you know how it goes.
                Thanks again!
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Couldn't you add the value to the getCollection array argument? Something like
                  $tvresources = $modx->getCollection('modTemplateVarResource', array('tmplvarid' => 36,'value' => $query));

                  That way no need for the foreach loop to check for the value.
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 36924 ☆ A M B ☆
                    • 298 Posts
                    I'm currently searching any area (including TVs) of a project underdevelopment. What found at first was when you specify the TV property of SimpleSearch TV search doesn't work. But when you take off that property it's able to search everywhere, including Tvs. [ed. note: ojchris last edited this post 13 years, 6 months ago.]
                      • 40045
                      • 534 Posts
                      Quote from: sottwell at Apr 09, 2013, 09:04 AM
                      Couldn't you add the value to the getCollection array argument? Something like
                      $tvresources = $modx->getCollection('modTemplateVarResource', array('tmplvarid' => 36,'value' => $query));

                      That way no need for the foreach loop to check for the value.

                      good point =), I think this should also do it, but the foreach loop would still be necessary to get the id and create the output right?

                      @ojchris, that's an interesting finding! Will definitely come handy! [ed. note: exside last edited this post 13 years, 6 months ago.]