zopyx
(Andreas Jung)
March 24, 2016, 1:05pm
1
we're using the following code for getting hold of the sitemap. However the code
seems to generate the sitemap from the navigation or site root inside of the current folder.
Is there a way to limit the generation of the sitemap to the current subtree?
context = aq_inner(self.context)
view = getMultiAdapter((context, self.request),
name='sitemap_builder_view')
data = view.siteMap()
-aj
ebrehault
(Eric Bréhault)
March 24, 2016, 1:42pm
2
The sitemap implementation is difficult to adpat (in my opinion), so I often find easier to write a custom BrowserView, like this:
class LocalSiteMap(BrowserView):
template = ViewPageTemplateFile('static/overrides/localsitemap.pt')
def __init__(self, context, request):
"""
"""
self.context = context
self.request = request
def get_children(self, path, depth):
if depth==3:
return None
results = []
entries = self.context.portal_catalog.searchResults({
'path': {'query': path, 'depth': 1},
'review_state': "published",
'is_default_page': False,
}, sort_on='getObjPositionInParent')
for entry in entries:
data = {'entry': entry}
children = None
if entry.portal_type in ['Folder', 'FolderMeteo']:
children = self.get_children(path+'/'+entry.id, depth+1)
if children:
data['children'] = children
results.append(data)
return results
def __call__(self):
catalog = self.context.portal_catalog
depth = 0
root = "/".join(self.context.getPhysicalPath())
entries = self.get_children(root, 0)
return self.template(entries=entries)
As you can see, it is quite specific (max depth = 3, and only recurse on few content types), but that's just an example.
But maybe there is a smarter way to do this kind of thing.
1 Like