<?php
/* fiveUp/fourUp/threeUp/twoUp/oneUp/self */
/* get parent template (one up) */
$oneUp= $resource->get('parent');
if ($oneUp != '') {
$oneUpObject = $modx->getObject('modResource',$oneUp);
$templateOneUp = $oneUpObject->get('template');
$aliasOneUp = $oneUpObject->get('alias');
/* get grandparent (two up) */
$twoUp = $oneUpObject->get('parent');
if ($twoUp !='') {
$twoUpObject = $modx->getObject('modResource',$twoUp);
$templateTwoUp = $twoUpObject->get('template');
$aliasTwoUp = $twoUpObject->get('alias');
/* get greatgrandparent (three up) */
$threeUp = $twoUpObject->get('parent');
if ($threeUp !='') {
$threeUpObject = $modx->getObject('modResource',$threeUp);
$templateThreeUp = $threeUpObject->get('template');
$aliasThreeUp = $threeUpObject->get('alias');
/* get ubergrandparent (four up) */
$fourUp = $threeUpObject->get('parent');
if ($fourUp !='') {
$fourUpObject = $modx->getObject('modResource',$fourUp);
$templateFourUp = $fourUpObject->get('template');
$aliasFourUp = $fourUpObject->get('alias');
/* get five up */
$fiveUp = $fourUpObject->get('parent');
if ($fiveUp !='') {
$fiveUpObject = $modx->getObject('modResource',$fiveUp);
$templateFiveUp = $fourUpObject->get('template');
$aliasFiveUp = $fiveUpObject->get('alias');
$last = 'fiveUp';
} else { $last = 'fourUp'; } // no fiveup
} else { $last = 'threeUp'; } // no fourup
} else { $last = 'twoUp'; } // no threeup
} else { $last = 'oneUp'; } // no twoup
} else { $last = 'self'; } // no oneup
// default
$newAliasOneUp = '';
$newAliasTwoUp = '';
$newAliasThreeUp = '';
$newAliasFourUp = '';
$newAliasFiveUp = '';
// create the aliases with slash if they exist
switch ($last)
{
case 'oneUp': $newAliasOneUp = $aliasOneUp.'/';
break;
case 'twoUp': $newAliasOneUp = $aliasOneUp.'/';
$newAliasTwoUp = $aliasTwoUp.'/';
break;
case 'threeUp': $newAliasOneUp = $aliasOneUp.'/';
$newAliasTwoUp = $aliasTwoUp.'/';
$newAliasThreeUp = $aliasThreeUp.'/';
break;
case 'fourUp': $newAliasOneUp = $aliasOneUp.'/';
$newAliasTwoUp = $aliasTwoUp.'/';
$newAliasThreeUp = $aliasThreeUp.'/';
$newAliasFourUp = $aliasFourUp.'/';
break;
case 'fiveUp': $newAliasOneUp = $aliasOneUp.'/';
$newAliasTwoUp = $aliasTwoUp.'/';
$newAliasThreeUp = $aliasThreeUp.'/';
$newAliasFourUp = $aliasFourUp.'/';
$newAliasFiveUp = $aliasFiveUp.'/';
break;
}
// if one of the parents has the hidden template (5), change the alias to nothing
if ($templateOneUp == '5') { $newAliasOneUp = ''; }
if ($templateTwoUp == '5') { $newAliasTwoUp = ''; }
if ($templateThreeUp == '5') { $newAliasThreeUp = ''; }
if ($templateFourUp == '5') { $newAliasFourUp = ''; }
if ($templateFiveUp == '5') { $newAliasFiveUp = ''; }
$alias = $resource->get('alias');
$newUri = $newAliasFiveUp.$newAliasFourUp.$newAliasThreeUp.$newAliasTwoUp.$newAliasOneUp.$alias;
$resource->set('uri_override', '1');
$resource->set('uri', $newUri);
$resource->save();<?php
$e = $modx->Event;
$resourceId = $e->params['id']; // Get the ID of resource
$q = $modx->newQuery('modResource'); // Fetch the parents
$q->where(array('id:IN' => $modx->getParentIds($resourceId, 10, 'web'))); /* 10 = depth, 'web' = context */
$parents = $modx->getCollection('modResource', $q);
foreach($parents as $parent) {
if ($parent->get('template') != 5) { // If not the hidden template
$out[] = $parent->get('alias');
}
}
$newUri = implode('/', $out) . "/" . $resource->get('alias');
$resource->set('uri_override', '1');
$resource->set('uri', $newUri);
$resource->save();
[ed. note: lazylegs last edited this post 14 years, 10 months ago.]
<?php
if ($mode != modSystemEvent::MODE_NEW) {
return '';
}
$q = $modx->newQuery('modResource'); // Fetch the parents
$q->where(array('id:IN' => $modx->getParentIds($resource->get('id']), 10, 'web'))); /* 10 = depth, 'web' = context */
$parents = $modx->getCollection('modResource', $q);
foreach($parents as $parent) {
if ($parent->get('template') != 5) { // If not the hidden template
$out[] = $parent->get('alias');
}
}
$newUri = implode('/', $out) . "/" . $resource->get('alias');
$resource->set('uri_override', '1');
$resource->set('uri', $newUri);
$resource->save();
<?php
$tempParent = $resource->get('parent'); //"cache" the parent
$tempParents = $modx->getParentIds($tempParent, 10, array('context' => 'web')); // depth = 10 and context = web
$tempParents[] = $tempParent; // Include the parent into array()
$q = $modx->newQuery('modResource'); // Fetch the parents
$q->where(array('id:IN' => $tempParents));
$parents = $modx->getCollection('modResource', $q);
foreach($parents as $parent) {
if ($parent->get('template') != 5) { // If not the hidden template
$out[] = $parent->get('alias');
}
}
$newUri = implode('/', $out) . "/" . $resource->get('alias'); // If you want, add . ".html" in to end
$resource->set('uri_override', '1');
$resource->set('uri', $newUri);
$resource->save();
[ed. note: lazylegs last edited this post 14 years, 10 months ago.]