<![CDATA[ getChildren (simple example of xPDO) - My Forums]]> https://forums.modx.com/thread/?thread=31993 <![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-535194 ]]> BobRay Dec 02, 2015, 02:20 PM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-535194 <![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-535171
$docId = $modx->resource->get('id');
$tpl = $modx->getOption('tpl',$scriptProperties,'');
if (!$tpl) { 
    return 'No template given.'; 
}
 
$c = $modx->newQuery('modResource');
$c->where(array('parent' => $docId, 'deleted' => 0, 'published' => 1));
$c->sortby('publishedon', 'ASC');
 
$docs = $modx->getCollection('modResource', $c);
 
$output = '<h2>[[*pagetitle]]</h2><ul>'; 
 
foreach ($docs as $doc) {
    $fields = $doc->toArray();
    $output .= $modx->getChunk($tpl, $fields);
}
 
$output .= '</ul>';
 
return $output;
]]>
konsensus Dec 02, 2015, 08:18 AM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-535171
<![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-534590
If there are no grandchildren, this will be significantly faster:

$docId = $modx->resource->get('id');
$tpl = $modx->getOption('tpl',$scriptProperties,'');
if (!$tpl) { 
    return 'No template given.'; 
}

$c = $modx->newQuery('modResource');
$c->where(array('parent' => $docId));
$c->sortby('publishedon', 'DESC');

$docs = $modx->getCollection('modResource', $c);

$output = '<h2>[[*pagetitle]]</h2><ul>'; 

foreach ($docs as $doc) {
    $fields = $doc->toArray();
    $output .= $modx->getChunk($tpl, $fields);
}

$output .= '</ul>';

return $output;


If there are grandchildren, you can do it like this:

$c = $modx->newQuery('modResource');
$c->sortby('publishedon', 'DESC');
$docs = $modx->resource->getMany('Children', $c);

]]>
BobRay Nov 19, 2015, 04:19 PM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-534590
<![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-534574
If you need that, use getResources or pdoResources.
Also migxLoopCollection, bundled with MIGX would do it, or rowboat.

for publishedon you will need

[[+publishedon]]


[[*publishedon]]

does allways get the value from the current resource.
]]>
Bruno17 Nov 19, 2015, 05:30 AM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo?page=2#dis-post-534574
<![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-534569
Hope you can help me with this:

QUESTION 1 – Sort by "publishedOn"
-----
I've been implementing this feature (Mark Hamster, Reply #1 in this thread) with success, but need to sort by "publishedOn".

I already tried to insert
&sortby=`{"publishedon":"DESC"} 
in my template with no luck.

Looks like this
[[press? &tpl=`tplBlog` &sortby=`{"publishedon":"DESC"}`]]


Whats wrong?

QUESTION 2 – How to output "publishedOn" for each children
-----

In addition – when I output "publishedon" at my frontend with this in my chunk:
<p>[[*publishedon]] - [[+content]]</p>
...

...all my children prints the "publishedon" for the parent, which is the same date for all children. How do I output the "publishedon"-date for each child?



My code is here:


SNIPPET - "press"
---
<?php
// First fetch all the children of the current resource
  $children = $modx->resource->getMany('Children');
  // Check if there are any. If not, return false
  if (!$children) { return false; }
  // Check if there is a tpl set, if not return an error
  $tpl = $modx->getOption('tpl',$scriptProperties,'');
  if (!$tpl) { return 'No template given.'; }
   
  // Start the output
  $o = '<h2>[[*pagetitle]]</h2><ul>'; 
  // Look through the results...
  foreach ($children as $child) {
    // ... fetching the needed info...
    $out = array(
      'id' => $child->get('id'),
      'pagetitle' => $child->get('pagetitle'),
      'menutitle' => $child->get('menutitle'),
      'content' => $child->get('content'),
      'longtitle' => $child->get('longtitle'));
    // ... and adding it to the output as placeholders in the chunk
    $o .= $modx->getChunk($tpl,$out);
  }
  // Don't forget to close the list
  $o .= '</ul>';
 
  // Return the output
  return $o;



CHUNK - tplBlog
---
<p>[[+content]]</p>
<p>[[*publishedon]]</p>



TEMPLATE - press
---
[[press? &tpl=`tplBlog` &sortby=`{"publishedon":"DESC"}`]]
]]>
konsensus Nov 19, 2015, 02:52 AM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-534569
<![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172958 Quote from: Mark at Feb 25, 2011, 11:19 AM

Shouldn’t that be getMany()?
Yes, it should. embarrassed]]>
BobRay Feb 26, 2011, 02:59 AM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172958
<![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172957 markh Feb 25, 2011, 05:19 AM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172957 <![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172956 Quote from: Mark at Feb 24, 2011, 11:44 PM

I just encountered an issue with this simple example, it doesn't check the hidemenu field so it displays everything, and not those which are checked to display in the menu :p

Could just check for that in the foreach loop, and do nothing if it aint set.

Right. I think getChildIds() will get all children including those that are not published or hidden from menus. I believe it will also get deleted children if the tree hasn't been purged.

As Mark H., says, though, you're going to need a foreach loop to display them anyway and it's easy enough to check the hidemenu, published, and deleted fields before adding them to the output.

If you use $resource->getMany('Children'), you can filter them in the call, but then you don't get to set the depth:

<?php
$resource->getMany('Children', array('hidemenu'=>'0', 'published'=>'1', 'deleted'=>'0'));
]]>
BobRay Feb 24, 2011, 08:36 PM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172956
<![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172955
Could just check for that in the foreach loop, and do nothing if it aint set.]]>
markh Feb 24, 2011, 05:44 PM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172955
<![CDATA[Re: getChildren (simple example of xPDO)]]> https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172954
$modx->getChildIds($id, $depth);
wink]]>
BobRay Feb 24, 2011, 02:21 PM https://forums.modx.com/thread/31993/getchildren-simple-example-of-xpdo#dis-post-172954