This question has been answered by dubbs. See the first response.
$thisId = $modx->resource->get('id'); $ids = array_reverse($modx->getParentIds($thisId)); $ids[] = $thisId; $i = 1; $level = $this->resource->getTVValue('levelsUp'); foreach ($ids as $id => $value) { $docIds[$i] = $value; if ($value == $docId) { $target = $docIds[$i - $level]; if (!empty($target)) { $url = $modx->makeUrl($docIds[$i - $level], "", "", "full"); if (!empty($url)) { $modx->sendRedirect($url); } } } $i++; } return '';
This is a kind of hacky way to do it, but I think it will work:
$thisId = $modx->resource->get('id'); $ids = array_reverse($modx->getParentIds($thisId)); $ids[] = $thisId; $i = 1; $level = $this->resource->getTVValue('levelsUp'); foreach ($ids as $id => $value) { $docIds[$i] = $value; if ($value == $docId) { $target = $docIds[$i - $level]; if (!empty($target)) { $url = $modx->makeUrl($docIds[$i - $level], "", "", "full"); if (!empty($url)) { $modx->sendRedirect($url); } } } $i++; } return '';
I should mention that unless you put some kind of conditional statement in the snippet to prevent it from executing in some cases, no one will ever see the pages with the snippet tag again unless the TV is modified.
[[redirectoParent? &levelsUp=`[[*levelsUp]]`]]
$thisId = $modx->resource->get('id'); $ids = array_reverse($modx->getParentIds($thisId)); $ids[] = $thisId; $i = 1; $level = $this->resource->getTVValue('levelsUp'); $output = '<p>In snippet id = ' . $thisId . '</p>'; foreach ($ids as $id => $value) { $docIds[$i] = $value; if ($value == $docId) { $target = $docIds[$i - $level]; if (!empty($target)) { $url = $modx->makeUrl($target, "", "", "full"); if (!empty($url)) { $output .= '<p>URL: ' . $url; // $modx->sendRedirect($url); } else { $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>'; } } else { $output .= return '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>'; } } $i++; } $output .= '<br><pre>' . print_r($ids, true) . '</pre>'; return $output;
$thisId = $modx->resource->get('id'); $ids = array_reverse($modx->getParentIds($thisId)); $ids[] = $thisId; $output .= '<br><pre>' . print_r($ids, true) . '</pre>'; return $output; return $thisId;
$i = 1; $level = $this->resource->getTVValue('levelsUp'); $output = '<p>In snippet id = ' . $thisId . '</p>'; foreach ($ids as $id => $value) { $docIds[$i] = $value; if ($value == $docId) { $target = $docIds[$i - $level]; if (!empty($target)) { $url = $modx->makeUrl($target, "", "", "full"); if (!empty($url)) { $output .= '<p>URL: ' . $url; // $modx->sendRedirect($url); } else { $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>'; } } else { $output .= return '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>'; } } $i++; }
$thisId = $modx->resource->get('id'); $ids = array_reverse($modx->getParentIds($thisId)); $ids[] = $thisId; $i = 1; // fixed here $level = $modx->resource->getTVValue('levelsUp'); $output = '<p>In snippet id = ' . $thisId . '</p>'; foreach ($ids as $id => $value) { $docIds[$i] = $value; if ($value == $docId) { $target = $docIds[$i - $level]; if (!empty($target)) { $url = $modx->makeUrl($target, "", "", "full"); if (!empty($url)) { $output .= '<p>URL: ' . $url; // $modx->sendRedirect($url); } else { $output .= '<p>URL is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>'; } } else { // fixed here $output .= '<p>Target is empty<br>Level is ' . $level . '<br> i is ' . $i . '</p>'; } } $i++; } $output .= '<br><pre>' . print_r($ids, true) . '</pre>'; return $output; return $thisId;
In snippet id = 67 Target is empty Level is 2 i is 1 Array ( [0] => 0 [1] => 11 [2] => 12 [3] => 62 [4] => 67 )
if ($value == $docId) {
if ($value == $thisId)
$thisId = $modx->resource->get('id'); $level = $modx->resource->getTVValue('levelsUp'); $ids = array_reverse($modx->getParentIds($thisId)); $ids[] = $thisId; foreach ($ids as $key => $value) { if ($value == $thisId) { $target = $docIds[$key - $level]; if (!empty($target)) { $url = $modx->makeUrl($target, "", "", "full"); if (!empty($url)) { $output .= '<p>URL: ' . $url; // $modx->sendRedirect($url); } else { $output .= '<p>URL is empty<br>Level is ' . $level . 'target is ' . $target . '</p>'; } } else { $output .= '<p>Target is empty<br>Level is ' . $level . 'target is ' . $target . '</p>'; } } } $output .= '<br><pre>' . print_r($ids, true) . '</pre>'; return $output; return $thisId;
Change this:
if ($value == $docId) {
to this:
{if ($value == $thisId)
BTW, I think it can also be simplified to this:
$thisId = $modx->resource->get('id'); $level = $modx->resource->getTVValue('levelsUp'); $ids = array_reverse($modx->getParentIds($thisId)); $ids[] = $thisId; foreach ($ids as $key => $value) { if ($value == $thisId) { $target = $docIds[$key - $level]; if (!empty($target)) { $url = $modx->makeUrl($target, "", "", "full"); if (!empty($url)) { $output .= '<p>URL: ' . $url; // $modx->sendRedirect($url); } else { $output .= '</p><p>URL is empty Level is ' . $level . 'target is ' . $target . '</p>'; } } else { $output .= '<p>Target is empty Level is ' . $level . 'target is ' . $target . '</p>'; } } } $output .= ' <pre>' . print_r($ids, true) . '</pre>'; return $output; return $thisId;
Target is empty Level is 2target is Array ( [0] => 0 [1] => 11 [2] => 12 [3] => 62 [4] => 67 )