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
    I think you’ll have to use @EVAL return runsnippet(’MySnippet’);

    Then use runsnippet(’getResources’) in your snippet and massage the return value to produce the correct input option values string.
      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
      • 8898
      • 181 Posts
      By chance I tried to accomplish exactly the same thing yesterday evening. And like Nomark I tried everything that’s possible and some impossible things, too, to get this working. getResources, Wayfinder and even things like $modx->getChildIds() etc. failed (no result) although they worked perfectly in a Resource or a Template.

      Finally I unpacked the primitive tools and did it like that:

      First, I created a Snippet I called SiteStructureForTv. I looks like this:

      <?php
      
      /**
       * This Snippet is called by the Manager (TV definition)
       * and can't be moved to a file. Also, Snippets like
       * getResources or Wayfinder aren't able to do the work here.
       * The same applies to $modx->getChildIds() etc. - it just
       * remains empty...
       */
      
      function getTvSiteStructure($dbh, $parentId = 0, $level = 1, $maxLevel = 2)
      {
          global $table_prefix;
      
          if ($level > $maxLevel) {
              return '';
          }
      
          $indent          = '--';
          $thisLevelIndent = (1 == $level) ? '' : str_repeat($indent, $level - 1).' ';
      
          $sql = "SELECT
                      `id`,
                      `menutitle`
                  FROM
                      `".$table_prefix."site_content`
                  WHERE
                      `parent`        = :parent
                      AND `published` = :published
                      AND `hidemenu`  = :hidemenu
                      AND `deleted`   = :deleted
                  ORDER BY
                      `menuindex`";
          $stmt = $dbh->prepare($sql);
          $params = array(
              ':parent'    => $parentId,
              ':published' => 1,
              ':hidemenu'  => 0,
              ':deleted'   => 0
          );
          if ($stmt->execute($params)) {
              $menuItems = $stmt->fetchAll();
          }
      
          $tvSiteStructure = '';
      
          foreach ($menuItems as $menuItem) {
              $tvSiteStructure .= $thisLevelIndent;
              // Although the charset is used when connecting to the database,
              // utf8_encode() is necessary here (?)
              $tvSiteStructure .= utf8_encode($menuItem['menutitle']).'=='.$menuItem['id'].'||';
      
              $tvSiteStructure .= ($level == $maxLevel) ? '' : getTvSiteStructure($dbh, $menuItem['id'], $level + 1, $maxLevel);
          }
      
          return $tvSiteStructure;
      }
      
      
      
      global $database_type,
             $database_server,
             $dbase,
             $database_user,
             $database_password,
             $database_connection_charset;
      
      $dsn =  $database_type.':host='.$database_server
              .';dbname='.trim($dbase, '`')
              .';charset='.$database_connection_charset;
      
      try {
          $dbh = new PDO($dsn, $database_user, $database_password);
      } catch (PDOException $e) {
          echo 'Connection failed: '.$e->getMessage();
      }
      
      $tvSiteStructure = getTvSiteStructure($dbh, 0, 1, 5);
      
      // Remove pipe symbols at the end of the string
      $tvSiteStructure = trim($tvSiteStructure, '|');
      
      return $tvSiteStructure;

      It’s not even possible to include this from a file; you have to enter this in MODx directly!

      In my Template Variable AssociatedPages I chose "checkbox" as input type; the input option values field looks like this:

      @EVAL return $modx->runSnippet('SiteStructureForTv');

      Doing this it works for me - finally! rolleyes

      I’m sure this can be solved more elegantly (e. g. via xPDO), but for now I have lost enough time with it; maybe I’ll look at this later. If someone finds a better solution, please feel free to post it! wink

      Cheers,
      Jan
        This message has been ROT-13 encrypted twice for higher security.
        • 24812
        • 7 Posts
        Nomark, You need to add &context=`web` in your getResources call to get it working. I’ve spent some time on this.

        eg. @EVAL return $modx->runSnippet(’getResources’,array(’parents’ => ... , ’context’ => ’web’);

        The solution does not work when you have multiple contexts and want to list resources only in edited document’s context. I’m pulling my hair for couple days now ;-) It is caused by that TVs are loaded by AJAX call and TV renderer has no idea which context document being edited belongs to.

        I’ve written custom tv input, and it is easy to get context_key when you edit existing document, still no luck when you create new document. In Revo 2.0.0 I used kind of hack - I extracted context key from $_REQUEST[’ys-modx-resource-tree’], but now the tree, can have multiple branches expanded so it is not reliable any more.

        Maybe Dev team will help us, PLZ smiley
          • 8898
          • 181 Posts
          Quote from: remek at Oct 12, 2010, 11:02 AM

          Nomark, You need to add &context=`web` in your getResources call to get it working. I’ve spent some time on this.

          eg. @EVAL return $modx->runSnippet(’getResources’,array(’parents’ => ... , ’context’ => ’web’);
          Ah, great tip, I’ll try that as soon as I find some time.

          Cheers,
          Jan
            This message has been ROT-13 encrypted twice for higher security.
          • Quote from: remek at Oct 12, 2010, 11:02 AM

            Nomark, You need to add &context=`web` in your getResources call to get it working. I’ve spent some time on this.

            eg. @EVAL return $modx->runSnippet(’getResources’,array(’parents’ => ... , ’context’ => ’web’);

            I never thought of that, thanks alot remek!
              Sterc Internet & Marketing | MODX Founding Partner | Chairman of the MODX Advisory Board

              In need of a MODX consult? Try our MODX Developers Experts!
              • 30703
              • 51 Posts
              Does this method of calling getResources via the @EVAL work? I’m currently trying to do the same thing but it’s not working, I’ve changed the context to ’web’ which I presumed was what I was initially missing but it’s still not working.

              My @EVAL code is:
              @EVAL return $modx->runSnippet('getResources', array('parents'=>'94', 'showUnpublished'=>'1', 'showHidden'=>'1', 'hideContainers'=>'1', 'tplFirst'=>'tvInputOptionsFirstTpl', 'tpl'=>'tvInputOptionsTpl', 'context'=>'web', 'limit'=>'0'));
              


              The ’parents’ 94 refers to a resource that contains ALL the videos I need to output...

              The chunk templates below:

              tvInputOptionsFirstTpl:
              [[+pagetitle]]==[[+id]]​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
              


              tvInputOptionsTpl:
              ||[[+pagetitle]]==[[+id]]​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
              


              When called, the code after the @EVAL returns:
              How To Video One==47 ||How To Video Two==51 ||How To Video Three==52 ||How To Video Four==53 ||How To Video Five==61 ||How To Video Six==62 ||How To Video Seven==63 ||How To Video Eight==64 ||How To Video Nine==65 ||How To Video Ten==67 ||How To Video Eleven==68 ||How To Video Twelve==70 ||How To Video Thirteen==71 ||How To Video Fourteen==73 ||How To Video Fifteen==74 ||How To Video Sixteen==75 ||How To Video Seventeen==76 ||HHow To Video Eighteen==83 ||How To Video Nineteen==84 ||How To Video Twenty==85 ||How To Video Twenty One==86 ||How To Video Twenty Two==87
              


              I have tried using this php code and calling it elsewhere and it works fine.
              Also, if I copy and paste the outputted text into the "Input Option Values" it shows me the exact options I require ... but I can’t get it working when using with the @EVAL binding.

              Anyone have any ideas why? sad

                Tom Dyer

                Designer/Animator/Web Developer - England, UK.
                http://www.bespokepixels.co.uk
                • 30703
                • 51 Posts
                After many attempts, I’ve come to the conclusion that this just isn’t possible with getResources at the moment sad

                I ended up using enigmatic_user’s code (with a couple of tweaks) - and it now works perfectly, kudos to you enigmatic_user smiley
                many thanks!

                  Tom Dyer

                  Designer/Animator/Web Developer - England, UK.
                  http://www.bespokepixels.co.uk
                  • 8930
                  • 44 Posts
                  Hi,

                  I created a variant. Here it’s :
                  <?php
                  
                  /* SetTVList
                   *
                   * Set a list from set of resources
                   *
                   * @author EQUILIBRE WEBDESIGN - Enzo GRAFTIEAUX
                   * @copyright 2011
                   * @version 0.2
                   * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License
                   */
                  
                  /* Get parameters */
                  $parentId = $modx->getOption('parent', $scriptProperties, 0);
                  $templateId = $modx->getOption('templateid', $scriptProperties, '');
                  $field = $modx->getOption('field', $scriptProperties, 'pagetitle');
                  if (!$templateId) return false;
                  
                  /* Select resources */
                  $fields = array('id', $field);
                  /*  Prepate SQL request */
                  $criteria = $modx->newQuery('modResource');
                  $criteria->where(array(
                      'template' => $templateId,
                      'published' => true,
                      'deleted' => false,
                  ));
                  /* SQL request*/
                  $resources = $modx->getCollection('modResource', $criteria);
                  
                  $output="";
                  /* Loop on resources found */
                  foreach ($resources as $resource){
                      $docid=$resource->get('id');
                      $fieldValue=$resource->get($field);
                      $output .= $fieldValue."==".$docid."||";
                  }
                  
                  /* Return result */
                  return rtrim($output,'|');
                  ?>


                  In the TV, set entries option values to :
                  @EVAL return $modx->runSnippet('setTVList',array("parent"=>"8","field" =>"pagetitle","templateid"=>"9"));


                    Equilibre Webdesign - Conception et r
                    • 30703
                    • 51 Posts
                    Hi enzo2.0,

                    I realise this post is from a while ago, but I was wondering how I use the &parent parameter since I can't see where it is used in your code...

                    I need to get the resources from a particular container, and currently this lists ALL resources, from the web context. How do I edit your code to actually handle the &parent property?

                    I was using the code earlier in the post but since I upgraded to MODX Revo 2.2 it stopped working. Yours seems to be working except for this one issue...

                    Thanks in advance....

                    Tom

                      Tom Dyer

                      Designer/Animator/Web Developer - England, UK.
                      http://www.bespokepixels.co.uk
                    • Bryte Digital Dialogues Reply #20, 11 years, 5 months ago
                      Hi,

                      It´s already 2 weeks ago wink but did you get it working for a particular container?

                      Thanks!



                      Quote from: trymedo at Jan 11, 2012, 05:38 PM
                      Hi enzo2.0,

                      I realise this post is from a while ago, but I was wondering how I use the &parent parameter since I can't see where it is used in your code...

                      I need to get the resources from a particular container, and currently this lists ALL resources, from the web context. How do I edit your code to actually handle the &parent property?

                      I was using the code earlier in the post but since I upgraded to MODX Revo 2.2 it stopped working. Yours seems to be working except for this one issue...

                      Thanks in advance....

                      Tom
                        Web development (Barcelona, Spain)

                        ..............................................
                        Bryte Digital Dialogues
                        ..............................................
                        Calle Nicaragua 48 5º 5ª,
                        08029 Barcelona, Spain

                        [email protected]
                        T +34 93 419 64 39

                        www.bryte.es
                        www.bryte.nl