Reactions Work v1.2.6
Add Facebook/linked style reactions to your Craft CMS content
More Craft plugins

API-SDK / Reactions Work service

The Reactions Work service

At the core of access to reactions work in twig, is the reactions work service (facade).
Retrieve it like this (the comment is for PHPStorm IDE with Symfony plugin for code completion)


{% set reactions_work = craft.reactions_work %}
{# reactions_work \twentyfourhoursmedia\reactionswork\services\ReactionsWorkFacade #}

results in:

getReactionHandles

Return all enabled reaction handles.


{{ dump(reactions_work.reactionHandles) }}
array(7) { ["like"]=> string(4) "like" ["love"]=> string(4) "love" ["haha"]=> string(4) "haha" ["wow"]=> string(3) "wow" ["sad"]=> string(3) "sad" ["angry"]=> string(5) "angry" ["custom1"]=> string(6) "amazed" }

isReactable

Checks if a user is allowed to react on a Craft Element


{% if reactions_work.isReactable(currentUser, entry) %}
  {# show the form or something %}
{% endif %}

react / unreact / toggle

Submits a reaction for a user to an element (or unreact - undo the reaction)
Returns the reactions model for the element


{% do reactions_work.react('amazed', entry, currentUser) %}
{% set reactions = reactions_work.react('amazed', entry, currentUser) %}

{% do reactions_work.unreact('amazed', entry, currentUser) %}

{# reverses the amazed reaction #}
{% do reactions_work.toggle('amazed', entry, currentUser) %}

getReactions

Returns the reactions model for an element


{% set reactions = reactions_work.reactions(entry) %}

getUsersFromIds

Helper function to retrieve some users from their id's in the original order


{% set reactions = reactions_work.reactions(entry) %}
{% set users = reactions_work.usersFromIds(reactions.allUserIds | slice(0,2)) %}
{% for user in users %}{{ user.name }} {% endfor %}

getFormUrl / getAjaxFormUrl

Return a form url to post reactions to for a certain element.
getFormUrl expects a redirect, while getAjaxFormUrl returns json data.

Create a full form with all available reaction handles:


<form method="post" action="{{ reactions_work.formUrl(element, currentUser) }}">
            {{ csrfInput() }}
            {{ redirectInput(craft.app.request.url) }}
            <input type="hidden" name="react" value="set" />
            {% for handle in reactions_work.reactionHandles %}
                {% if not canReact or reactions.canRegister(handle, currentUser) %}
                    <button name="reaction" {{ disabledAttr }} type="submit" class="btn btn-info" value="{{ handle }}">{{ handle }}</button>
                {% endif %}

                {% if reactions.canDeregister(handle, currentUser) %}
                    <button name="reaction" {{ disabledAttr }} type="submit" class="btn btn-danger" value="{{ handle }}">{{ handle }}</button>
                {% endif %}
            {% endfor %}
        </form>