Examples

# Users Following / Follow List

Follow is a flexible plugin to allow you to follow any Elements in Craft CMS. But the main usecase for the plugin is that you can create a list of users that another user is following and a list of followers of that user also. This is a very basic example similar to Twitter etc using Follow.

# Getting the user

Firstly, allow the currentUser to follow another user by getting the users profile. You can easily do this by dynamically adding a user ID to the craft.users query to get the user to follow:

{% set userToFollow = craft.users.id(1).one() %}

Or (The preferred way), by setting a route in config/routes.php to allow users to have profiles such as http://website.com/@username:

'@<username:[^\/]+>' => ['template' => 'user/_profile']

Then get the username variable from the URL segment and add it to the craft.users query:

{% set userToFollow = craft.users.username(username).one() %}

# User Profile

Template: user/_profile

{% set user = craft.users.username(username).one() %}

<h1>{{ user.firstName }}'s Profile</h1>

{% if currentUser %}

    {% if craft.follow.check({ elementId: user.id }) %}
        <a href="{{ craft.follow.unfollowUrl(user.id) }}">Unfollow {{ user.firstName }}</a>
    {% else %}
        <a href="{{ craft.follow.followUrl(user.id) }}">Follow {{ user.firstName }}</a>
    {% endif %}

{% endif %} 

<h2>
    Followers
    ({{ craft.follow.followersTotal(user.id) }})
</h2>

{% set followerUserIds = craft.follow.followers(user.id) %}

{% for user in craft.users.id(followerUserIds) %}
    {{ user.fullName }}
{% endfor %}

<h2>
    Following
    ({{ craft.follow.followingTotal({ userId: user.id }) }})
</h2>

{% set followingUserIds = craft.follow.following({ userId: user.id }) %}

{% for user in craft.users.id(followingUserIds).all() %}
    {{ user.fullName }}
{% endfor %}


# Newsfeed

Another great example for Follow is the ability to allow users to follow a particular category, and output a feed of Entries that are posted in that category.

# Following Categories

Assume you have a nature blog, with the category group nature which has the categories Birds, Animals & Plants. You want to allow the user to follow Plants, and when you post entries in to the Plants category, they appear on their feed. But not show entries for Nature and Animals:

Template: blog/categories

{% if currentUser %}

    {% for category in craft.categories.group('nature').all() %}

        {% if craft.follow.check({ elementId: category.id }) %}
            <a href="{{ craft.follow.unfollowUrl(category.id) }}">Unfollow {{ category.title }}</a>
        {% else %}
            <a href="{{ craft.follow.followUrl(category.id) }}">Follow {{ category.title }}</a>
        {% endif %}

    {% endfor %}

{% endif %}

# Entries Feed

Template: blog/feed

{% if currentUser %}

    <h1>Nature News Feed</h1>

    {% set params = {
        elementClass: 'craft\\elements\\Category',
        output: 'array'
    } %}

    {% set natureCategories = craft.follow.following(params) %}

    {% for entry in craft.entries.section('nature').relatedTo(natureCategories).all() %}
        <a href="{{ entry.url }}">{{ entry.title }}</a>
    {% endfor %}

{% endif %}

# Favourite Authors

(Coming Soon)

# Commerce Product Updates

(Coming Soon)

# Wishlist / Save For Later / Bookmark

It's possible to make Follow act as a Wishlist, where a user could save an Entry, Asset etc to a saved list. This will allow the user to see a list of all saved elements within their profile. The only limitation to this is that they can only have 1 list, so a Wishlist plugin might be better suited.

# Favourite, Save or Bookmark an element

Firstly, let the user save the element, whether this is an Entry, Asset or Category etc. Just simply pass the elements ID. For this example we'll assume they are saving recipes to read later:

Template: recipes/index

{% if currentUser %}

    {% for entry in craft.entries.section('recipes').all() %}

        {% if craft.follow.check({ elementId: entry.id }) %}
            <a href="{{ craft.follow.unfollowUrl(entry.id) }}">Remove</a>
        {% else %}
            <a href="{{ craft.follow.followUrl(entry.id) }}">Save for later</a>
        {% endif %}

    {% endfor %}

{% endif %}

# Saved elements

Next, output a list of recipes the user has saved, in to a page on their profile etc.

Template: user/saved-recipes

{% if currentUser %}

    {% set params = {
        elementClass: 'craft\\elements\\Entry'
    } %}

    {% set recipeEntryIds = craft.follow.following(params) %}

    <h1>Saved Photos</h1>

    <h2>You have {{ craft.follow.followingTotal(params) }} saved recipes</h2>

    {% for entry in craft.entries.section('recipes').id(recipeEntryIds).all() %}
        {{ entry.title }} - <a href="{{ craft.follow.unfollowUrl(entry.id) }}">Remove</a>
    {% endfor %}

{% endif %}