Blog Sections Open
Showing Image Thumbnails in AjaxSearch Results with a Custom PHx Modifier
Use a lightweight existence check before printing thumbnails in AjaxSearch output so missing files do not leave broken markup in search results.
One common AjaxSearch customization is to show a thumbnail next to every search result. The catch is that the image is not always stored in a TV, and sometimes the file may not exist at all. In that case the result template needs a safe condition instead of a blind <img> tag.
The Original Pattern
The legacy topic used a result template that tried to build the image path from the resource ID and then pass it through a custom PHx modifier:
<div class="[+as.resultClass+]">
[+phx:`/assets/images/thumbs/[+as.id+]_small.jpg`:file_exists+]
<a class="[+as.resultLinkClass+]" href="[+as.resultLink+]" title="[+as.longtitle+]">[+as.pagetitle+]</a>
[+as.descriptionShow:is=`1`:then=`
<span class="[+as.descriptionClass+]">[+as.description+]</span>
`+]
[+as.extractShow:is=`1`:then=`
<div class="[+as.extractClass+]"><p>[+as.extract+]</p></div>
`+]
[+as.breadcrumbsShow:is=`1`:then=`
<span class="[+as.breadcrumbsClass+]">[+as.breadcrumbs+]</span>
`+]
<div class="clear"></div>
</div>
The custom modifier itself looked like this:
<?php
/*
* description: Check if file exists
* usage: [+filename:file_exists=`String to return if exists`+]
*/
$url = trim($output);
$output = $modx->config['base_path'] . $url;
if (file_exists($output)) {
return 'EXIST';
}
return 'NOT EXIST';
Why This Often Fails in AjaxSearch
AjaxSearch runs its own parser flow for result placeholders. If you bolt a PHx modifier into the template, the modifier may run, but the value can still be overwritten later in AjaxSearch output processing. That makes the final result look empty even though the file check itself worked.
A More Reliable Approach
If you need this behavior in production, the safer option is to calculate one explicit result field before the final template is rendered. In practice that means one of two things:
- extend the AjaxSearch parser with a dedicated modifier case such as
imgexists - prepare the full image markup in PHP and output a ready-made placeholder instead of stacking parser layers
The legacy discussion even tried a direct parser case like this:
case 'imgexists':
if ($output != '' && file_exists($modx->config['base_path'] . $output)) {
$output = 1;
} else {
$output = 0;
}
break;
Practical Recommendation
For maintainable search results, keep the rule simple: build the path, check it once in PHP, and expose either a ready imageUrl or a ready thumbnailHtml placeholder to the result template. That is easier to debug than chaining PHx inside AjaxSearch internals.
If you still prefer the PHx route, test directly inside assets/snippets/ajaxSearch/classes/asPhxParser.class.inc.php and verify that no later step resets the placeholder value.
Sending Shopkeeper Order Status Emails for Completed Orders
How to extend a Shopkeeper workflow so status changes such as Completed still trigger a customer email, not only the default states.
Making TinyMCE Insert BR Instead of Paragraph Tags on Enter
A quick TinyMCE configuration tweak for projects that really need line breaks on Enter instead of automatic paragraph wrappers.