diff --git a/src/Form/RestoreForm.php b/src/Form/RestoreForm.php index b55e0f16519310a481320345cb87e53ad19fbeb6..166ab9648b4d5aa64cc9d2172a9f68578afcd52b 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 729d25be9dfe6e310dbc9476dc1b9634e2cec0a5..d537bec6eb29dd5b5a6e0252a7d31e29996f6981 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 374651cfd1628a06e424cccae098f33e1f04338a..afdc9caa7f25a89e8479402c0d2dac2e33ec1f2a 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); }