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

    Is there a GetField snippet equivalent in Revo?
      • 22295
      • 153 Posts
      Here’s a simple example called resourceinfo,
      can be used as a snippet [[resourceinfo? &input=`510` &options=`content`]]
      or as an output filter: [[+pageId:resourceinfo=`content`]]
      If the requested field was not found (or is empty) it will try to find this field as a TV.

      <?php
      $id = $scriptProperties['input'];
      if (empty($id) || !is_numeric($id)) return;
      $field = isset($scriptProperties['options']) ? $scriptProperties['options'] : 'pagetitle';
      $output = '';
      
      $resource = $modx->getObject('modResource',$id);
      if (empty($resource)) return;
      $output = $resource->get($field);
      if (empty($output)) $output = $resource->getTVValue($field);
      
      return $output;
      ?>
        • 20413
        • 2,877 Posts
        Thanks for sharing!! The community is the engine cylinders of MODx!
          @hawproductions | http://mrhaw.com/

          Infograph: MODX Advanced Install in 7 steps:
          http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

          Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
          http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
          • 22303 MODX Staff
          • 10,725 Posts
          For comparison, here is a Snippet I created called getValue that can get a single field from any xPDOObject derivative class (i.e. any tables represented by an xPDO model), using JSON to describe the criteria. It does not work as a filter, but this is a very efficient way to get a single field from an object based on a set of criteria, and optionally provide a default value if nothing is returned by the query:
          <?php
          $value = !empty($default) ? $default : '';
          if ($criteria = $modx->newQuery($class, $modx->fromJSON($where))) {
              $pk = $modx->getPK($class);
              if (!is_array($pk)) {
                  $pk = array($pk);
              } else {
                  $pk = array_values($pk);
              }
              $criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, array($field))));
              if ($object = $modx->getObject($class, $criteria)) {
                  $value = $object->get($field);
              }
          }
          return $value;
          ?>

          The key difference is instead of selecting every single column of the table and returning all that data across the pipe from MySQL to create the object, here I am only selecting the requested field (and the primary key field( s ), which is required of any xPDOObject query). This reduces both the amount of time it takes to query and pass the data from the MySQL server, and the amount of memory used by PHP when that data is returned.

          Also, the fromJSON() allows the representation of just about any criteria when requesting the data. Some examples:
          [[getValue? &class=`modUser` &field=`username` &where=`{"id":[[+createdby]]}` default=`anonymous`]]
          [[getValue? &class=`modResource` &field=`content` &where=`{"id":[[+id]]}`]]
          [[getValue? &class=`modResource` &field=`id` &where=`{"alias":"foobar","isfolder":1}`]]
          
            • 5340
            • 1,624 Posts
            Hi OpenGeek,

            I’m trying to use your code to do this

            <?php
            $parentId = $modx->resource->get('parent');
            
            $tv = $modx->getObject('modTemplateVar', array('name' =>'ArticlesBanner'));
            
            echo $tv->getValue($parentId);
            ?>


            Can you assist me in doing the above using your snippet?

            So far I have
            [[getValue? &class=`modTemplateVar` &field=`ArchiveTitle` default=`Archive`]]


            The snippet should output the default value but unfortunately it doesn’t work.


            Thank you!
              • 22303 MODX Staff
              • 10,725 Posts
              Getting Template Variable values is not really the intended use of getValue, though you could use it to get specific modTemplateVarResource raw values. The code you are using is the appropriate solution.