From 308cf6be646f010ace8ec749426b157e7f076a6a Mon Sep 17 00:00:00 2001
From: Tim Millwood <tim@millwoodonline.co.uk>
Date: Wed, 14 Sep 2016 17:42:25 +0100
Subject: [PATCH] Adding Restore to TrashManager

---
 src/Form/RestoreForm.php      | 26 +++++++++++++++-----------
 src/TrashManager.php          | 12 +++++++++++-
 src/TrashManagerInterface.php | 10 +++++++++-
 3 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/src/Form/RestoreForm.php b/src/Form/RestoreForm.php
index b55e0f1..166ab96 100644
--- a/src/Form/RestoreForm.php
+++ b/src/Form/RestoreForm.php
@@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Form\ConfirmFormBase;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Url;
+use Drupal\trash\TrashManagerInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
 
@@ -28,12 +29,20 @@ class RestoreForm extends ConfirmFormBase {
    */
   protected $entityTypeManager;
 
+  /**
+   * The Trash Manager service.
+   *
+   * @var \Drupal\trash\TrashManagerInterface
+   */
+  protected $trashManager;
+
   /**
    * {@inheritdoc}
    */
   public static function create(ContainerInterface $container) {
     return new static(
-      $container->get('entity_type.manager')
+      $container->get('entity_type.manager'),
+      $container->get('trash.manager')
     );
   }
 
@@ -42,9 +51,12 @@ class RestoreForm extends ConfirmFormBase {
    *
    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
    *   The entity type manager.
+   * @param \Drupal\trash\TrashManagerInterface $trash_manager
+   *   The Trash Manager service.
    */
-  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
+  public function __construct(EntityTypeManagerInterface $entity_type_manager, TrashManagerInterface $trash_manager) {
     $this->entityTypeManager = $entity_type_manager;
+    $this->trashManager = $trash_manager;
   }
 
   /**
@@ -104,15 +116,7 @@ class RestoreForm extends ConfirmFormBase {
    * {@inheritdoc}
    */
   public function submitForm(array &$form, FormStateInterface $form_state) {
-    $content_moderation_states = $this->entityTypeManager
-      ->getStorage('content_moderation_state')
-      ->loadByProperties([
-        'content_entity_type_id' => $this->entity->getEntityTypeId(),
-        'content_entity_id' => $this->entity->id(),
-      ]);
-    $content_moderation_state = reset($content_moderation_states);
-    $content_moderation_state->moderation_state->target_id = 'draft';
-    if ($content_moderation_state->save()) {
+    if ($this->trashManager->restore($this->entity)) {
       drupal_set_message(t('The @entity "@label" has been restored.', [
         '@entity' => $this->entity->getEntityType()
           ->get('label'),
diff --git a/src/TrashManager.php b/src/TrashManager.php
index 729d25b..d537bec 100644
--- a/src/TrashManager.php
+++ b/src/TrashManager.php
@@ -2,14 +2,24 @@
 
 namespace Drupal\trash;
 
+use Drupal\Core\Entity\ContentEntityInterface;
+
 class TrashManager implements TrashManagerInterface {
 
   /**
    * {@inheritdoc}
    */
-  public function trash($entity) {
+  public function trash(ContentEntityInterface $entity) {
     $entity->set('moderation_state', 'archived');
     return (bool) $entity->save();
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function restore(ContentEntityInterface $entity) {
+    $entity->set('moderation_state', 'draft');
+    return (bool) $entity->save();
+  }
+
 }
diff --git a/src/TrashManagerInterface.php b/src/TrashManagerInterface.php
index 374651c..afdc9ca 100644
--- a/src/TrashManagerInterface.php
+++ b/src/TrashManagerInterface.php
@@ -2,6 +2,8 @@
 
 namespace Drupal\trash;
 
+use Drupal\Core\Entity\ContentEntityInterface;
+
 interface TrashManagerInterface {
 
   /**
@@ -9,6 +11,12 @@ interface TrashManagerInterface {
    *
    * @return bool
    */
-  public function trash($entity);
+  public function trash(ContentEntityInterface $entity);
 
+  /**
+   * @param \Drupal\Core\Entity\ContentEntityInterface $entity
+   *
+   * @return bool
+   */
+  public function restore(ContentEntityInterface $entity);
 }
-- 
GitLab