Sottwell is right (as always). What I described relates to nested snippet calls, i.e. snippet tags used as parameters in another snippet tag.
Try it with:
<?php
$pageId = isset($pageId) ? $pageId : $modx->documentIdentifier;
$parentObject = $modx->getParent($pageId);
$pid=$parentObject['id'];
?>
or:
<?php
$pid=$modx->documentObject['parent'];
?>
Sorry again. My call to getParent() was incomplete. You have to tell it what you want. Note the change in the first line:
<?php
/* get parent id */
$parentObject = $modx->getParent($pageId,0,'id');
$pid=$parentObject['id'];
switch ($pid) {
case '1':
return $modx->getChunk('chunk1');
break;
case '2':
return $modx->getChunk('chunk1');
break;
}
return $modx->getChunk('defaultChunk');
I think getParent returns an array of the parent-ressource and you have to call the value(s) with its key:
$parentObject = $modx->getParent($pageId);
$pid=$parentObject['id'];
http://wiki.modxcms.com/index.php/API:getParent
Quote from: Bruno17 at Aug 26, 2009, 03:20 AM
I think getParent returns an array of the parent-ressource and you have to call the value(s) with its key:
$parentObject = $modx->getParent($pageId);
$pid=$parentObject['id'];
http://wiki.modxcms.com/index.php/API:getParent
Correctomundo. My bad. Ended up using this (which definitely works) :
$parentObject = $modx->getParent($pageId,0,'id');
$pid=$parentObject['id'];
Glad you got it working.
BTW, if you want documents further down in the tree to use the selected sidebar chunk, I think you can do it with the UltimateParent snippet:
http://modxcms.com/extras/package/?package=475.
[!GetSidebar? &pageId=`[[UltimateParent]]` !]
Then in the snippet:
$pid = $pageId;
switch etc. (code you have now)
You know, the more I think about this, the more I think a single template variable (TV) with a default value of @INHERIT is a better solution, especially if there are a lot of pages at various levels. That way, every page will use the sidebar chunk of its ultimate parent unless you set it to something else.
Just create a template variable called SidebarTV, with an input Type of text and a default output type. Give it the default value of :@INHERIT name_of_default_chunk.
For the Input Options Value, either leave it blank or use a list of all the sidebar chunk names like this:
chunk1||chunk2||chunk3
If you use the names, change the input type to radio buttons or drop-down list.
Assign the TV to the template used for the pages.
Replace your current GetSidebar tag with this:
{{[*SidebarTV*]}}
Now, when you edit a page, you’ll see the SidebarTV and can set it to the name of the chunk you want. All children of that page at any level will use the same chunk unless you modify the TV for them.
Hope this makes sense.
In Input Options:
{{chunk1}}||{{chunk2}}||{{chunk3}}
and in template
[*SidebarTV*]
should work