pepebe
4/20/2012 - 8:52 PM

MODx: Return all siblings of a resource

MODx: Return all siblings of a resource

<?php
// The catch here is to exclude the resource itself from the list of children of the parent resource.
// Useful for wayfinder snippets, because wayfinders only knows how to exclude things.
//
// Usage:
// [[siblings]] or [[siblings? &parent=`[[*parent]]` &exclude=`[[*id]]`]]
//
// Parameters:
// &parent  = parent resource id                     (default: parent of current resource)
// &exclude = comma seperated list of ids to exclude (default: current resource id)

$id = $modx->resource->get('id');

  if(!$parent)  { $parent = $modx->resource->get('parent'); }
  if(!$exclude) { $exclude = $modx->resource->get('id'); }
  
  // all children of parent element
  $children = $modx->getChildIds($parent, 1);

  // excude id from array
  $siblings = array_diff( $children, array($exclude) );
  // create comma seperated string
  $siblings = implode(",", $siblings);

  return $siblings;