We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40131
    • 40 Posts
    I am wondering how to list many parents based on a TV value on their children:
    So we have initially:
    Parent1
    - child_01

    - child_N
    Parent2
    - child_01

    - child_N
    Parent_N etc.

    I want to list only the parent(s) based on its children, if a child has a TV with value set to 1. If there are no children with the TV=1, no parent should be listed.
    So, If there one or more children, the relevant parent must be listed. I don't want to list the children, only the parents.

    Any ideas to accomplish with getResources or pdoResources?

    This question has been answered by nickyz. See the first response.

    [ed. note: nickyz last edited this post 7 years, 6 months ago.]
      • 3749
      • 24,544 Posts
      I don't think you'll find a way to do this with an existing extra. I think you'll need a custom snippet.

      I was going to suggest some code, but I'm confused by your description of what you want to do.

      I'm probably just being dense, but your specifications seem to contradict each other:

      "I want to list only the parent(s) and its children"
      "no parent should be listed"
      "the relevant parent must be listed"
      "I don't want to list the children, only the parents"

      It's not clear to me when those various rules apply.
        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
        • 40131
        • 40 Posts
        Oh Bob, I am sorry for misunderstanding. I have edited the bad expression. As a conclusion only parents list. I have many product categories. A lot of them have discontinued products (children with a TV=1), other parents don't have any. So we need to list only the categories which contain discontinued products. Later when I have these category ids I could list the products...


        Quote from: BobRay at Jun 02, 2017, 04:50 PM
        I don't think you'll find a way to do this with an existing extra. I think you'll need a custom snippet.

        I was going to suggest some code, but I'm confused by your description of what you want to do.

        I'm probably just being dense, but your specifications seem to contradict each other:

        "I want to list only the parent(s) and its children"
        "no parent should be listed"
        "the relevant parent must be listed"
        "I don't want to list the children, only the parents"

        It's not clear to me when those various rules apply.
        [ed. note: nickyz last edited this post 7 years, 6 months ago.]
          • 3749
          • 24,544 Posts
          Thanks. I think I get it now.

          BTW, your case is exactly why you should never use TVs to store data that will be used for searching and sorting. wink

          If you could put the discontinued flag in a resource field you're not using (introtext, description, longtitle, donthit, privateweb, or privatemgr), it would make things simpler and faster. The last three don't show up in the Manager (they are no longer used by MODX), but if the other fields are in use, you could just add a plugin attached to OnDocFormSave, that wrote your existing TV's value to the hidden field:

          /* Discontinued Plugin */
          $tvId = 12; // change this to the ID of the TV holding the 1
          
          if ($resource->getTVValue($tvID) == 1) {
             $resource->set('donthit', '1');
             $resource->save();
          }


          Obviously, it would be more convenient for users if you could use one of the first three and just rename its caption in Lexicon Management. In that case, change 'donthit' in the code below to the name of the field you use and you won't need the plugin.

          Then, you could display the parents with something like this:

          $output = '';
          $parentArray = array();
          $disField = 'donthit';
          
          $docs = $modx->getCollection('modResource', array($disField => 1));
          
          
          foreach ($docs as $doc)  {
             $parentId = (int) $doc->get('parent');
             $parentObj = $modx->getObject('modResource', $parentId);
             $parentPagetitle = $parentObj->get('pagetitle');
          
             if (! in_array($parentPageTitle, $parentArray)) {
                 $parentArray[] = $parentPagetitle;
             } 
          }
          /* Put them in alphabetical order */
          natcasesort($parentArray);
          
          /* create the output */
          $output = '<ul>';
          
          foreach($parentArray as $pagetitle) {
             $output .= '<li class="disItem">' . $pagetitle . '</li>';
          }
          
          
          
          $output .= '</ul>';
          
          return $output;


          Using the TV alone, the code would be several times as long and very complicated.

          This would be much faster using $modx->getObjectGraph(), but it would require a lot of testing to make sure it works. It should also use a Tpl chunk or two.
            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
          • discuss.answer
            • 40131
            • 40 Posts
            Thanks Bob! I am sure your solution is much more elegant and speedier. Unfortunately I am not a PHP programmer and I needed more flexibility because my site has more contexts and more resources to utilize and display.
            Finally I have found a solution with modx common snippets to do the job.

            Here is my approach:
            main page:
            [[pdoResources:default=``?
             &parents=`[[UltimateParent]]`
             &depth=`1`
             &includeContent=`0`
             &tpl=`CategoryListerItemDP`
             &templates=`6`
             &limit=`0` 
             &showHidden=`0` 
             &sortby=`{"pagetitle":"ASC"}`
            ]]


            [[$CategoryListerItemDP]]:

            [[pdoResources:default=``?
              &parents=`[[+id]]`
             &includeContent=`0`
             &includeTVs=`DiscProd`
             &tpl=`@INLINE [[+idx]]`
             &templates=`7`
             &tvFilters=`DiscProd==1`
             &limit=`1` 
             &showHidden=`0` 
             &sortby=`{"pagetitle":"ASC"}`
             &toPlaceholder=`catsDP`
            ]]
            
            [[!If?
               &subject=`[[+catsDP]]`
               &operator=`GT`
               &operand=`0`
               &then=`<h2 class="redcolor">[[+pagetitle]]:</h2><ul class="clearfix">[[!$prodDP? &idp=`[[+id]]`]]</ul>`
               &else=``
            ]]


            [[$prodDP]] has another pdoResources call to list all applicable products.

            It works great and speed is just fine as well. [ed. note: nickyz last edited this post 7 years, 6 months ago.]
              • 3749
              • 24,544 Posts
              I'm glad you got it sorted. Thanks for reporting your solution. 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