Manipulating simpleSearch results

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?

what about unchecking the searchable checkboxes?

It think the error occurs for resources with no matching parent.

$id = $resource->get('parent');
$resource = $modx->getObject('modResource',$id);
$template = $resource->get('template');

On the root level, when the parent is 0, the variable $id is set to 0. getObject() returns null (because there is no resource with this id) and then you try to call get() on null, which causes the error.

1 Like

You also need to check if the function “isPageTemplate” already exists:

if (!function_exists('isPageTemplate')) {
    function isPageTemplate ($tplId) {
        ...
    }
}
1 Like

The searchable checkboxes for folders are unchecked, but almost all other resources need to be searchable.

I changed the snippet as follows and it works now… thank you @halftrainedharry :

<?php
if (!function_exists('isPageTemplate')) {
    function isPageTemplate ($tplId) {
        $allowedTemplates = array(2, 30, 34, 40, 58);
        $isPageTemplate = in_array($tplId, $allowedTemplates);
        return $isPageTemplate;
    }
}
$homepage = $modx->getOption('site_start');
if ($id > 0) {
    $resource = $modx->getObject('modResource',$id);
    $template = $resource->get('template');
    while (!isPageTemplate($template) && $id > 0 && $template > 0) {
        $id = $resource->get('parent');
        $resource = $modx->getObject('modResource',$id);
        $template = $resource->get('template');
    } 
    if ($id > 0 && $template > 0) {
        return $id;
    } else {
        return $homepage;
    }
} else {
    return $homepage;
}

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.