Answers.Body: <p>You have 2 solutions:</p>
  <ul>
  <li>Implement your own <code>Restore</code> action, or</li>
  <li>Be lazy and reuse <code>\craft\elements\actions</code></li>
  </ul>
  <p>I won't cover implementing you own action, let's be lazy!</p>
  <h3>Declare the action</h3>
  <p>To do this, you need to override the <code>defineActions()</code> method of your element's class:</p>
  <pre><code>protected static function defineActions(string $source = null): array
  {
      $actions = parent::defineActions($source);
  
      // Restore
      $actions[] = Craft::$app-&gt;getElements()-&gt;createAction([
          'type' =&gt; \craft\elements\actions\Restoreclass,
          'successMessage' =&gt; Craft::t('your-module', 'Elements restored.'),
          'partialSuccessMessage' =&gt; Craft::t('your-module', 'Some elements restored.'),
          'failMessage' =&gt; Craft::t('your-module', 'Elements not restored.'),
      ]);
  
      return $actions;
  }
  </code></pre>
  <p>Craft will automatically hide the restore action when the selected status is not <strong>Trashed</strong> (<a href="https://github.com/craftcms/cms/blob/20a3c7990e2ff9b7be87bc939b7a46de6c3b579a/src/controllers/ElementIndexesController.php#L583-L591" rel="nofollow noreferrer">here's how</a>).</p>
  <p>The <strong>Restore</strong> action is now shown when viewing the <strong>Trashed</strong> elements.
  <a href="https://i.sstatic.net/pVqWy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/pVqWy.png" alt="enter image description here" /></a></p>
  <p>But using it will result in a 500 error <br />
  Let's fix that.</p>
  <h3>Add an <code>essentials</code> scenario to your element</h3>
  <p><code>craft\elements\actions</code>, tries to validate the elements being restored using the <code>essentials</code> scenario. If your element doesn't have such scenario, restoration will fail.</p>
  <p>To add a new scenario, you need to override your element's <code>scenarios()</code> method:</p>
  <pre><code>public function scenarios()
  {
      $scenarios = parent::scenarios();
      $scenarios[self::SCENARIO_ESSENTIALS] = [ 
          // list here the name of attributes to validate when restoring
      ];
  
      return $scenarios;
  }
  </code></pre>
  <p>You should now be able to restore element </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-16T10:07:37.783Z
Answers.LastActivityDate: 2021-11-16T10:07:37.783Z
Answers.UserDisplayName: nstCactus
Body: <p>In Craft CMS 3, custom element types support soft-deletion out of the box.</p>
  <p>But once soft-deleted, there is now way to restore elements that are now listed in the <strong>Trashed</strong> status, the only available action is <strong>Delete permanently</strong>.</p>
  <p><a href="https://i.sstatic.net/NwfFB.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/NwfFB.png" alt="enter image description here" /></a></p>
  <p>How can you make custom elements restorable?</p>
  
Tags: <craft3><modules><elementaction><soft-deletes>
Title: How to make custom element types restorable?
ThreadText: # How to make custom element types restorable?
  
  (Asked by: username_0 on 2021-11-16)
  
  In Craft CMS 3, custom element types support soft-deletion out of the box.
  
  But once soft-deleted, there is now way to restore elements that are now listed in the **Trashed** status, the only available action is **Delete permanently**.
  
  [![enter image description here](https://i.sstatic.net/NwfFB.png)](https://i.sstatic.net/NwfFB.png)
  
  How can you make custom elements restorable?
  
  **Tags:** craft3, modules, elementaction, soft-deletes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-11-16. Score: 4
  
  You have 2 solutions:
  
  * Implement your own `Restore` action, or
  * Be lazy and reuse `\craft\elements\actions`
  
  I won't cover implementing you own action, let's be lazy!
  
  ### Declare the action
  
  To do this, you need to override the `defineActions()` method of your element's class:
  
  ```
  protected static function defineActions(string $source = null): array
  {
      $actions = parent::defineActions($source);
  
      // Restore
      $actions[] = Craft::$app->getElements()->createAction([
          'type' => \craft\elements\actions\Restoreclass,
          'successMessage' => Craft::t('your-module', 'Elements restored.'),
          'partialSuccessMessage' => Craft::t('your-module', 'Some elements restored.'),
          'failMessage' => Craft::t('your-module', 'Elements not restored.'),
      ]);
  
      return $actions;
  }
  
  ```
  
  Craft will automatically hide the restore action when the selected status is not **Trashed** ([here's how](https://github.com/craftcms/cms/blob/20a3c7990e2ff9b7be87bc939b7a46de6c3b579a/src/controllers/ElementIndexesController.php#L583-L591)).
  
  The **Restore** action is now shown when viewing the **Trashed** elements.
  [![enter image description here](https://i.sstatic.net/pVqWy.png)](https://i.sstatic.net/pVqWy.png)
  
  But using it will result in a 500 error   
  Let's fix that.
  
  ### Add an `essentials` scenario to your element
  
  `craft\elements\actions`, tries to validate the elements being restored using the `essentials` scenario. If your element doesn't have such scenario, restoration will fail.
  
  To add a new scenario, you need to override your element's `scenarios()` method:
  
  ```
  public function scenarios()
  {
      $scenarios = parent::scenarios();
      $scenarios[self::SCENARIO_ESSENTIALS] = [ 
          // list here the name of attributes to validate when restoring
      ];
  
      return $scenarios;
  }
  
  ```
  
  You should now be able to restore element
  
  
  ---
  
  