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

    I have a tv with dropdown resource list input. With getresources extra is very easy to get any field or tv to display in the chosen resource page.

    But I would like to get all the resources in which a particular resource has been chosen. Is there a way to do that?

    Example:

    resource 2 ----dropdown resource list ----> resource 1
    resource 3 ----dropdown resource list ----> resource 1
    resource 4 ----dropdown resource list ----> resource 1

    I need in the resource 1 page to display a list of fields and tv's from resource 2, 3 and 4.

    Thank you in advance. [ed. note: ispasov last edited this post 12 years, 4 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Well, here's one way to get a list of resources with a TV that has a given value, such as the selected resource ID in your case...it could be modified to use the value of the current resource ID in the 'value' field, for example.
      $id = $modx->resource->get('id');
      $tvs = $modx->getCollection('modTemplateVarResource',array('tmplvarid'=>'1', 'value'=>$id));
      $output = "";
      foreach ($tvs as $tv) {
        $resource = $tv->get(array('contentid'));
        $output .= $resource['contentid'] . ",";
      }
      $output = rtrim($output, ",");
      return $output;

      So your getResources call would look like this
      [[getResources? &parents=`-1` &resources=`[[getResourceIds]]` ... ]]
      [ed. note: sottwell last edited this post 12 years, 4 months ago.]
        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        We could improve on this snippet by supporting a property for the desired value...
        <?php
        $val = $modx->getOption('value', $scriptProperties, $modx->resource->get('id'));
        // Important! Edit the line below so the 'tmplvarid' is the ID of your TV!
        $tvs = $modx->getCollection('modTemplateVarResource',array('tmplvarid'=>'1', 'value'=>$val));
        $output = "";
        foreach ($tvs as $tv) {
          $resource = $tv->get(array('contentid'));
          $output .= $resource['contentid'] . ",";
        }
        $output = rtrim($output, ",");
        return $output;

        And then the snippet would look like this
        [[pdoResources? &parents=`0` &resources=`[[getResourceIds? &value=`1`]]` &tpl=`@CODE <li>[[+pagetitle]]</li>`]]

        [ed. note: sottwell last edited this post 12 years, 4 months ago.]
          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
          • 46630
          • 40 Posts
          Well that is some advanced Modx programming for me.

          For my knowledge so far I should paste the first part of your code in a snipet and run the second from a template, or I am totally wrong?
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Yes, the first bit of code is for the snippet. The getResources (or pdoResource) snippet goes wherever you want the items displayed - could be a template, could be in the content of the parent resource.
              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
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              The snippet code -

              The first line gets the "value" property of the snippet (the &value part of the snippet call), setting a sane default of the current resource ID if no &value property is used. This is the resource that actually has the getResources snippet in it (or in its template).

              The second line gets a collection of TVs where the TV ID is #1 (you will need to edit this to be the actual ID of the TV you are using), and where its value is &val (either the &value in the snippet call, or the ID of the current resource). This comes from the site_tmplvar_contentvalues table in the database, which has the TV id, the id of the resource, and the value set for that resource. I really should add to the code to support another property, the name of the TV, but it was getting late and I was getting tired wink

              Since the getCollection() function returns an array of whatever objects we asked it for, we use the foreach() PHP looping structure to deal with each TV found, one at a time. We're converting each TV object to an array, and grabbing its "contentid" value - that is the ID of the resource where the specified value is set.

              Since we told it to add a comma after each one, we need to get rid of the last comma, so we tell PHP to trim off the right-most comma. We'll end up with a comma-separated list of all the resource IDs that have the TV set to our specified value.

              And that's exactly what the getResources snippet wants for its &resources property, a comma-separated list of resources to display. I believe you should be able to use &parents=`-1` in getResources to tell it to ignore the &parents property and only use &resources; that doesn't work in pdoResources. [ed. note: sottwell last edited this post 12 years, 4 months ago.]
                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
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Of course, your getResources tpl will be a chunk tpl with a lot more to it than I did for the simple demonstration.
                  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