Migrating from "Likes" to "Reactions"
If you currently have an add-on which implements a "Like" content handler you may be wondering how you migrate to "Reactions" in XF 2.1 and what will happen to your existing likes.
We have ensured that your add-on will continue to function as normal after upgrading to XF 2.1. This means that your existing like handler code, your existing like templates, alert templates for likes, entities, controllers and everything will continue to appear and function as it does now without you needing to make any changes.
In fact, if you'd like to, your add-on can continue to use likes going forward. That said, it would be safe to assume that the like system is now deprecated and it may be slated for removal at some point in the future. So it would be strongly recommended to embrace and implement the new reactions functionality as soon as possible.
But, the good news is, we've made it very simple to migrate to reactions through a number of different helpers.
AbstractSetup::migrateTableToReactions($tableName, $likesColumn = 'likes', $likeUsersColumn = 'like_users')
You would call this for each of your content types and pass in the table name where the changes need to apply. This renames the existing
likes
column to
reaction_score
, the existing
like_users
column to
reaction_users
and adds a new column named
reactions
(which stores a tally of which reactions have been applied to the content and how many times).
AbstractSetup::renameLikeAlertOptionsToReactions($contentTypes, $oldAction = 'like')
You can pass in an array of content types (or a single content type) here and this renames any "alert opt outs" to the new name. You will need to update any phrases related to alert opt outs too.
AbstractSetup::renameLikeAlertsToReactions($contentTypes, $renameNewsFeed = true, $oldAction = 'like')
This renames any existing "like" alerts to a "reaction" alert with the required extra data, and if enabled also any news feed entries. In addition to this, you will need to update any like alert/news feed templates. Use a core template such as
alert_post_reaction
and
news_feed_item_post_reaction
for reference.
AbstractSetup::renameLikePermissionsToReactions(array $permissionGroupIds, $likePermissionId = 'like')
Given an array of permission group IDs, this will rename any existing permission with an ID of
like
(or a custom ID) to
react
. The array you pass in should actually have the existing permission group ID as its key, and a
true
or
false
value to denote whether or not it is a content specific permission or global only, similar to this:
PHP:
$this->renameLikePermissionsToReactions([
'forum' => true, // global and content
'conversation' => false, // global only
'profile_post' => false // global only
]);
XF\Entity\ReactionTrait
trait
After removing most references to likes in your content entity, you will need to add in some new reaction specific stuff. To make this easier, we've added a new ReactionTrait. This will require you to implement a
canReact
method (which will mostly be the same as your existing
canLike
method).
Templates and controller actions
Similar to the existing
XF:Like
controller plugin, there is a new
XF:Reaction
controller plugin. These will help you very easily start supporting the user interface for likes. In fact, there are no custom templates to create at all for interacting with reactions as the controller plugin will call default templates for this purpose. You can check out
XF\Pub\Controller\Post
for examples.
The only thing you would need to change in your templates is the existing like link and like summary code. Even for these, we've got some new XF template tags to help. Check out the
post_macros
template for an example.