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).
The Restore action is now shown when viewing the Trashed elements.

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 🎉