Add a method to an existing element Trying to get my head around plugin development. Sorry if this is a super basic question. How do you add a method to an existing element? I’m trying to get any active Commerce 2 subscriptions for a user. I figured out how to add it as a variable to my plugin.

public function getActiveSubscriptions(int $userId): array
{
    return Subscription::find()
        -&gt;userId($userId)
        -&gt;status(Subscription::STATUS_ACTIVE)
        -&gt;all();
}


It’s called by {% set subscriptions = craft.myPlugin.getActiveSubscriptions(currentUser.id) %}

That works, but I feel like the “right” way is to be able to do this {% set subscriptions = currentUser.getActiveSubscriptions() %} How do I do that?
You can do that with Behaviors. There is a good Working example made by Pixel and Tonic team within their Git for the DotAll workshop

namespace ontherocks\behaviors;

use craft\elements\db\EntryQuery;
use craft\elements\Entry;
use craft\elements\User;
use yii\base\Behavior;

/**
 * Class UserBehavior
 *
 * @property User $owner
 * @property EntryQuery $recipes
 */
class UserBehavior extends Behavior
{
    public function getRecipes(): EntryQuery
    {
        if (!$this-&gt;owner-&gt;id) {
            // just return a query that is doomed to fail
            return Entry::find()
                -&gt;id(false);
        }
        return Entry::find()
            -&gt;section('recipes')
            -&gt;authorId($this-&gt;owner-&gt;id);
    }
}


See how to attach behaviors

// define user behavior
Event::on(User::class, Element::EVENT_DEFINE_BEHAVIORS, function(DefineBehaviorsEvent $event) {
    $event-&gt;behaviors[$this-&gt;id] = UserBehavior::class;
});


You can attach your custom behavior to the user class as well exactly like the code above and do

{% set user = currentUser %}
{% set subscriptions = user.getActiveSubscriptions() %}


Or you attach the behavior to the SubscriptionQuery and do

{% set subscriptions = craft.subcriptions.activeSubscriptions(currentUser).all() %}