Blog Sections Open

Using DocLister prepare to Enrich Rows with Data from Another Table

A practical DocLister pattern for pulling extra row data from custom tables without hard-coding everything into the template.

DocLister’s prepare callback is one of the cleanest ways to enrich row data before rendering. It is especially useful when the listing itself comes from one source, but related assets such as images live in another table.

Example setup

[!DocLister?
&controller=`avto`
&idType=`documents`
&prepare=`.getFotoAvto`
&tpl=`@CODE: ... [+image-src+] ...`
&display=`10`
&paginate=`pages`!]

The callback then reads a custom table and appends HTML or a derived value into the row data:

$result = $modx->db->select('*', 'my_img_avto');
while ($row = $modx->db->getRow($result)) {
    if ($row['id_ad'] == $data['id']) {
        $tmp .= '<img src="' . $modx->runSnippet('phpThumb', [
            'input' => $row['src'],
            'options' => 'w=150,h=76,far=C,bg=FFFFFF'
        ]) . '">';
    }
}
$data['image-src'] = $tmp;
return $data;

Why this helps

The template stays focused on presentation while the callback handles the extra lookup logic. That is much easier to maintain than scattering custom SQL and image logic directly into snippets or chunks.

Performance warning

If the callback scans a whole related table for every row, it will slow down on large datasets. For bigger projects, narrow the related query up front or pre-index the related records by the current document IDs.

Newer post

Restricting Frontend Resources by Web User Group in Evolution CMS

How to protect frontend resources in Evolution CMS by assigning them to web user groups instead of relying only on template logic.

Older post

Requiring at Least One Contact Field in eForm

How to validate an Evolution CMS eForm so that at least one of several contact fields is filled in, instead of requiring every field separately.