<![CDATA[ Parent(s) list based on children TV - My Forums]]> https://forums.modx.com/thread/?thread=102333 <![CDATA[Parent(s) list based on children TV]]> https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551434 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?
]]>
nickyz Jun 02, 2017, 01:46 PM https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551434
<![CDATA[Re: Parent(s) list based on children TV]]> https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551684 ]]> BobRay Jun 12, 2017, 08:34 PM https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551684 <![CDATA[Re: Parent(s) list based on children TV (Best Answer)]]> https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551668 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.]]>
nickyz Jun 12, 2017, 09:15 AM https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551668
<![CDATA[Re: Parent(s) list based on children TV]]> https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551457
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.]]>
BobRay Jun 02, 2017, 09:35 PM https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551457
<![CDATA[Re: Parent(s) list based on children TV]]> https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551450

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.
]]>
nickyz Jun 02, 2017, 04:59 PM https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551450
<![CDATA[Re: Parent(s) list based on children TV]]> https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551447
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.]]>
BobRay Jun 02, 2017, 04:50 PM https://forums.modx.com/thread/102333/parent-s-list-based-on-children-tv#dis-post-551447