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

Usage / Example

Simple example in Twig

This example shows reactions and a reaction form (if the user is logged in).
You have to supply an 'Element' which may be any craft element (Entry, User, Category, ...)


{# reactions \twentyfourhoursmedia\reactionswork\models\Recording #}
{# element \craft\base\Element #}
{% with %}
    {% set reactions_work = craft.reactions_work %}
    {# reactions_work \twentyfourhoursmedia\reactionswork\services\ReactionsWorkFacade #}

    <div class="alert alert-info">
        {% set total = reactions.countAllReactions %}
        <h3>{{ total }} reactions</h3>

        {% set canReact = reactions_work.reactable(currentUser, element) %}
        {% set disabledAttr = canReact ? '' : 'disabled="disabled"' %}

        Your reaction: {{ reactions.reaction(currentUser.id) }}

        <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>

        {% for handle in reactions_work.reactionHandles %}
            <table>
                <tr>
                    <td>
                        <strong>{{ handle }}</strong>
                    </td>
                    <td>
                        {{ reactions.countReactions(handle) }} reactions
                    </td>
                </tr>
            </table>
        {% endfor %}

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

    </div>
{% endwith %}