Answers.Body: <p>You can do that with <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-behaviors" rel="nofollow noreferrer">Behaviors</a>. There is a good Working example made by Pixel and Tonic team within their Git for the <a href="https://github.com/craftcms/ontherocks/blob/step-12/src/behaviors/UserBehavior.php" rel="nofollow noreferrer">DotAll workshop</a></p>
  
  <pre class="lang-php prettyprint-override"><code>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);
      }
  }
  </code></pre>
  
  <p>See how to attach behaviors</p>
  
  <pre class="lang-php prettyprint-override"><code>// define user behavior
  Event::on(User::class, Element::EVENT_DEFINE_BEHAVIORS, function(DefineBehaviorsEvent $event) {
      $event-&gt;behaviors[$this-&gt;id] = UserBehavior::class;
  });
  </code></pre>
  
  <p>You can attach your custom behavior to the user class as well exactly like the code above and do</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set user = currentUser %}
  {% set subscriptions = user.getActiveSubscriptions() %}
  </code></pre>
  
  <p>Or you attach the behavior to the <code>SubscriptionQuery</code> and do</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set subscriptions = craft.subcriptions.activeSubscriptions(currentUser).all() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-25T06:15:22.310Z
Answers.LastActivityDate: 2018-10-25T06:15:22.310Z
Answers.UserDisplayName: Robin Schambach
Body: <p>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.</p>
  
  <pre><code>public function getActiveSubscriptions(int $userId): array
  {
      return Subscription::find()
          -&gt;userId($userId)
          -&gt;status(Subscription::STATUS_ACTIVE)
          -&gt;all();
  }
  </code></pre>
  
  <p>It’s called by <code>{% set subscriptions = craft.myPlugin.getActiveSubscriptions(currentUser.id) %}</code></p>
  
  <p>That works, but I feel like the “right” way is to be able to do this <code>{% set subscriptions = currentUser.getActiveSubscriptions() %}</code> How do I do that?</p>
  
Tags: <plugin-development><craft3><plugin-craftcommerce>
Title: Add a method to an existing element
ThreadText: # Add a method to an existing element
  
  (Asked by: username_0 on 2018-10-24)
  
  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()
          ->userId($userId)
          ->status(Subscription::STATUS_ACTIVE)
          ->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?
  
  **Tags:** plugin-development, craft3, plugin-craftcommerce
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-10-25. Score: 2 | Accepted answer
  
  You can do that with [Behaviors](https://www.yiiframework.com/doc/guide/2.0/en/concept-behaviors). There is a good Working example made by Pixel and Tonic team within their Git for the [DotAll workshop](https://github.com/craftcms/ontherocks/blob/step-12/src/behaviors/UserBehavior.php)
  
  ```php
  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->owner->id) {
              // just return a query that is doomed to fail
              return Entry::find()
                  ->id(false);
          }
          return Entry::find()
              ->section('recipes')
              ->authorId($this->owner->id);
      }
  }
  
  ```
  
  See how to attach behaviors
  
  ```php
  // define user behavior
  Event::on(User::class, Element::EVENT_DEFINE_BEHAVIORS, function(DefineBehaviorsEvent $event) {
      $event->behaviors[$this->id] = UserBehavior::class;
  });
  
  ```
  
  You can attach your custom behavior to the user class as well exactly like the code above and do
  
  ```twig
  {% set user = currentUser %}
  {% set subscriptions = user.getActiveSubscriptions() %}
  
  ```
  
  Or you attach the behavior to the `SubscriptionQuery` and do
  
  ```twig
  {% set subscriptions = craft.subcriptions.activeSubscriptions(currentUser).all() %}
  
  ```
  
  
  ---
  
  