Blog Sections Open

Tracking Resource Views in a TV and Sorting Ditto by That Value

If you want “most viewed” listings in Evolution CMS, one practical approach is to store hits in a TV and sort the output by that saved value later.

This donor proposed a pragmatic solution for “popular content” blocks: write page view counts into a TV and then let Ditto sort documents by that saved value.

Example plugin logic

if ($modx->documentObject['donthit'] != 1 && $_SESSION['usertype'] != 'manager') {
  $current = $modx->getTemplateVarOutput('count', $modx->documentIdentifier);
  $current = $current['count'] + 1;
  setTemplateVar($current, $modx->documentIdentifier, 'count');
}

A practical implementation uses a helper to insert or update the TV value in site_tmplvar_contentvalues.

Important detail

The donor explicitly recommended using the plugin on OnWebPagePrerender. That keeps the counter update aligned with a real page render and avoids counting manager previews.

Recommendation

This is a workable legacy pattern when you need a quick popularity metric without introducing a separate analytics table. Just remember that it is still application-level counting, not a substitute for serious analytics.

Newer post

Combining autoFilter with request-Based Ditto Sorting and Pagination

When autoFilter and request-based Ditto parameters collide, the clean fix is to separate filtering from sorting concerns instead of trying to force one cached call to do everything.

Older post

Working with Checkbox Inputs in eForm

Checkboxes in eForm need explicit handling because an unchecked box sends nothing, while a checked one sends a value that still must pass validation.