
Reactions
Universal reactions for MODX 3 — likes, GitHub-style sets, tops and trending on any object


Reactions on Collections resources work the same as on plain modResource. Collections does not need a separate class_key.
[[!Reactions? &set=`github`]]{'!Reactions' | snippet : ['set' => 'github']}<article class="collection-item">
<h2><a href="[[~[[+id]]]]">[[+pagetitle]]</a></h2>
[[!ReactionsCount?
&object=`[[+id]]`
&format=`👍 {LIKES} · 💬 {TOTAL}`
]]
</article><article class="collection-item">
<h2><a href="{$id | url}">{$pagetitle}</a></h2>
{'!ReactionsCount' | snippet : [
'object' => $id,
'format' => '👍 {LIKES} · 💬 {TOTAL}',
]}
</article>Use pdoResources with a leftJoin on ReactionAggregate.
[[!pdoResources?
&parents=`[[*id]]`
&depth=`1`
&leftJoin=`{"Aggregate":{"class":"Reactions\\Model\\ReactionAggregate","on":"Aggregate.object_id = modResource.id AND Aggregate.object_class = 'modResource'"}}`
&sortby=`Aggregate.likes`
&sortdir=`DESC`
&tpl=`@INLINE <li><a href="[[+uri]]">[[+pagetitle]]</a> — [[!ReactionsCount? &object=`[[+id]]` &format=`{LIKES}`]]</li>`
]]{'!pdoResources' | snippet : [
'parents' => $_modx->resource.id,
'depth' => 1,
'leftJoin' => '{"Aggregate":{"class":"Reactions\\Model\\ReactionAggregate","on":"Aggregate.object_id = modResource.id AND Aggregate.object_class = \'modResource\'"}}',
'sortby' => 'Aggregate.likes',
'sortdir' => 'DESC',
'tpl' => '@INLINE <li><a href="[[+uri]]">[[+pagetitle]]</a> — [[!ReactionsCount? &object=`[[+id]]` &format=`{LIKES}`]]</li>',
]}Limit the selection by parent with pdoResources, not with TopLiked alone. TopLiked sorts globally by class_key and has no parent filter.
Option: pdoResources + sortby Aggregate.likes (see above).
Global top (all resources in the context):
[[!TopLiked?
&class=`modResource`
&period=`month`
&limit=`10`
&context=`web`
]]{'!TopLiked' | snippet : [
'class' => 'modResource',
'period' => 'month',
'limit' => 10,
'context' => 'web',
]}On a “Popular in this section” page:
<h2>Hot items</h2>
[[!pdoResources?
&parents=`[[*id]]`
&depth=`2`
&leftJoin=`{"Aggregate":{"class":"Reactions\\Model\\ReactionAggregate","on":"Aggregate.object_id = modResource.id AND Aggregate.object_class = 'modResource'"}}`
&sortby=`Aggregate.trending_score`
&sortdir=`DESC`
&limit=`6`
&tpl=`tpl.collection.trending`
]]<h2>Hot items</h2>
{'!pdoResources' | snippet : [
'parents' => $_modx->resource.id,
'depth' => 2,
'leftJoin' => '{"Aggregate":{"class":"Reactions\\Model\\ReactionAggregate","on":"Aggregate.object_id = modResource.id AND Aggregate.object_class = \'modResource\'"}}',
'sortby' => 'Aggregate.trending_score',
'sortdir' => 'DESC',
'limit' => 6,
'tpl' => 'tpl.collection.trending',
]}Add ReactionsSchema to the collection resource template.
[[!ReactionsSchema]]{'!ReactionsSchema' | snippet}Details: SEO and Schema.org.