SimpleSearch + Agenda

Is it possible to list/find events with SimpleSearch? I tried the customPackages but i have no Idea how to set the url/resource with joinCriteria

[[!SimpleSearch? &customPackages=AgendaEvents:title,content:agenda:{core_path}components/agenda/model/:joinCriteria = modResource.id]]

Hello @desper,
i’ve got the same some weeks ago. This ist my solution based on posthooks (https://github.com/modxcms/SimpleSearch/issues/22#issuecomment-160207265):

<?php

$calendarResourceId = $modx->getOption( 'calendar_id' );
$titlePrefix = '[[%simplesearch_facet_calendar_title_prefix? &namespace=`jens`]]';
$results = [];

$corePath = $modx->getOption('agenda.core_path', null, $modx->getOption('core_path') . 'components/agenda/');
$agenda = $modx->getService('agenda', 'Agenda', $corePath . 'model/agenda/', [
    'core_path' => $corePath
] );

$c = $modx->newQuery( 'AgendaEvents' );
$c->select( 'id, title' );
$c->where( [
    'title:LIKE' => '%' . $search . '%',
    'OR:description:LIKE' => '%' . $search . '%',
    'OR:content:LIKE' => '%' . $search . '%'
] );
$count = $modx->getCount( 'AgendaEvents', $c );
$c->limit( $limit, $offset );
$events = $modx->getCollection( 'AgendaEvents', $c );

foreach ( $events as $event ) {
    $results[] = [
        'pagetitle' => $titlePrefix . $event->get('title'),
        'longtitle' => '',
        'link' => $modx->makeUrl( $calendarResourceId, '', [
	        
        ] ),
        'excerpt' => '',
    ];
}

$hook->addFacet( 'agenda', $results, $count );
return true;
1 Like

This is great, thank you so much – it works now! The events now do appear in the search results. I just changed addFacet to default, so it get mixed with other results and changed the link to the event details page.

Only problem left, is that I get an error in my MODX error log for every agenda search result.

(ERROR @ ...core/model/modx/modx.class.php : 1031) [[+id]] is not a valid integer and may not be passed to makeUrl()
(ERROR in resource 606 @ .../core/model/modx/modparser.class.php : 1373) Bad link tag [[~[[+id]]]] encountered

<?php

$results = [];

$corePath = $modx->getOption('agenda.core_path', null, $modx->getOption('core_path') . 'components/agenda/');
$agenda = $modx->getService('agenda', 'Agenda', $corePath . 'model/agenda/', [
    'core_path' => $corePath
] );

$c = $modx->newQuery( 'AgendaEvents' );
$c->select( 'id, title' );
$c->where( [
    'title:LIKE' => '%' . $search . '%',
    'OR:content:LIKE' => '%' . $search . '%'
] );
$count = $modx->getCount( 'AgendaEvents', $c );
$c->limit( $limit, $offset );
$events = $modx->getCollection( 'AgendaEvents', $c );

foreach ( $events as $event ) {
    $eventlink = 'event=' . $event->get('id');
    $results[] = [
        'pagetitle' => $event->get('title'),
        'link' => $modx->makeUrl(110, '', $eventlink),
        'excerpt' => '',
    ];
}

$hook->addFacet( 'default', $results, $count );
return true;

I got that error also with your original code. Could you check if you get the same error in your logs? If not, I know I messed something up before I tried your solution.

Thank you again!

Could you post the content of the facet template chunk? Or do you use the same template as the main results?

The option is documented here: https://docs.modx.com/current/en/extras/simplesearch/simplesearch/faceted-search-through-posthooks#separate-templating-per-facet

1 Like

I didn’t use a custom tpl to this point, but that’s where the problem was coming from.

The standard tpl uses an output filter

href="[[+link:is=``:then=`[[~[[+id]]]]`:else=`[[+link]]`]]" 

that caused the problem. Using a custom tpl now with just [[+link]] works. Thanks @jako

I just need to find out now, how to use two different facets in the same SimpleSearch. Right now I put the hook into the default facet, but in this case I can’t give the agenda entries a different color for example. But that’s a different topic.

Here is the working code now.

snippet/posthook (agendaSearch):

<?php
//110 from $modx->makeUrl(110, '', $eventlink) is the event detail resource id
$results = [];

$corePath = $modx->getOption('agenda.core_path', null, $modx->getOption('core_path') . 'components/agenda/');
$agenda = $modx->getService('agenda', 'Agenda', $corePath . 'model/agenda/', [
    'core_path' => $corePath
] );

$c = $modx->newQuery( 'AgendaEvents' );
$c->select( 'id, title' );
$c->where( [
    'title:LIKE' => '%' . $search . '%',
    'OR:content:LIKE' => '%' . $search . '%'
] );
$count = $modx->getCount( 'AgendaEvents', $c );
$c->limit( $limit, $offset );
$events = $modx->getCollection( 'AgendaEvents', $c );

foreach ( $events as $event ) {
    $eventlink = 'event=' . $event->get('id');
    $results[] = [
        'pagetitle' => $event->get('title'),
        'link' => $modx->makeUrl(110, '', $eventlink),
        'excerpt' => '',
    ];
}

$hook->addFacet( 'default', $results, $count );
return true;

chunk (CustomSearchResult):

<div class="simplesearch-result">
    <h3>[[+idx]]. <a href="[[+link]]" title="[[+longtitle]]">[[+pagetitle]]</a></h3>
    <div class="extract">
        <p>[[+extract]]</p>
    </div>
</div>

Snippet Call

[[!SimpleSearch?
   &perPage=`10`
   &postHooks=`agendaSearch`
   &facetLimit=`5`
   &tpl=`CustomSearchResult`                              
]]
1 Like

Please try if the issue in the error log is solved by:

href="[[[[+link:is=``:then=`~[[+id]]`:else=`+link`]]]]"