I’m using simpleSearch with an AJAX call to display the results in a dropdown beneath the search field.
That works fine, but the site has many levels: pages, folders with pages, blocks and sub-blocks in pages etc.
The results from simplesearch include id’s for the resources found and I want to display these (links to) resource-id’s only if the resource has a ‘page’ template i.e. is not a (sub)block-resource or container-resource. Or else I want to find the parent page for it.
The AJAX call uses this this (simplified) snippet to display the results:
[[!SimpleSearch?
&tpl=`bncSearchResult`
]]
Then I have this chunk ‘bncSearchResult’ for the individual results:
<div class="sisea-result">
<h3><a class="jsSearchResultLink"
href="[[~[[!getParentPage? &id=`[[+id]]`]]]]">[[+pagetitle]]</a>
</h3>
<div class="extract"><p>[[+extract]]</p></div>
</div>
So I pass the found id’s to a snippet ‘getParentPage’ which searches up the tree until it finds a resource with a certain template id, which indicates it is a page and not any other kind of block.
The snippet works fine when I use in plainly in a page and I pass it an id of a nested block:
<?php
function isPageTemplate ($tplId) {
$allowedTemplates = array(2, 30, 34, 40, 58);
$isPageTemplate = in_array($tplId, $allowedTemplates);
return $isPageTemplate;
}
if (isset($id)) {
$resource = $modx->getObject('modResource',$id);
$template = $resource->get('template');
while ((!isPageTemplate($template)) && ($id > 0)) {
$id = $resource->get('parent');
$resource = $modx->getObject('modResource',$id);
$template = $resource->get('template');
}
return $id;
}
return 'no-id';
But it does not work in the search results, i.e. the AJAX call returns 500 errors. What am I missing?
(without the snippet getParentPage there are no errors and search results are shown)
I tried before to use pdoField in the chunk for the results to get the parent resources, but I can’t be sure about the number of levels I have to go up to reach a resource with a page template, hence this new attempt via the snippet ‘getParentPage’.
Or is there a better approach to this?