From fa54ef451c9ed9153548c04ab6341abfc32a1c99 Mon Sep 17 00:00:00 2001 From: Tim Millwood <tim@millwoodonline.co.uk> Date: Wed, 14 Sep 2016 15:19:43 +0100 Subject: [PATCH] adding trash method to TrashManager service --- src/Controller/TrashDeleteController.php | 22 ++++++++++++++++------ src/TrashManager.php | 8 ++++++++ src/TrashManagerInterface.php | 9 ++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/Controller/TrashDeleteController.php b/src/Controller/TrashDeleteController.php index f096624..5b7d7b7 100644 --- a/src/Controller/TrashDeleteController.php +++ b/src/Controller/TrashDeleteController.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityFormBuilderInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; +use Drupal\trash\TrashManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -44,6 +45,13 @@ class TrashDeleteController extends ControllerBase { */ protected $entityFormBuilder; + /** + * The Trash Manager service. + * + * @var \Drupal\trash\TrashManagerInterface + */ + protected $trashManager; + /** * Constructs a new TrashDeleteController object. * @@ -55,12 +63,15 @@ class TrashDeleteController extends ControllerBase { * The Content Moderation moderation information service. * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder * The Entity Form Builder service. + * @param \Drupal\trash\TrashManagerInterface $trash_manager + * The Trash Manager service. */ - public function __construct(RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information, EntityFormBuilderInterface $entity_form_builder) { + public function __construct(RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderation_information, EntityFormBuilderInterface $entity_form_builder, TrashManagerInterface $trash_manager) { $this->routeMatch = $route_match; $this->entityTypeManager = $entity_type_manager; $this->moderationInformation = $moderation_information; $this->entityFormBuilder = $entity_form_builder; + $this->trashManager = $trash_manager; } /** @@ -71,7 +82,8 @@ class TrashDeleteController extends ControllerBase { $container->get('current_route_match'), $container->get('entity_type.manager'), $container->get('content_moderation.moderation_information'), - $container->get('entity.form_builder') + $container->get('entity.form_builder'), + $container->get('trash.manager') ); } @@ -84,18 +96,17 @@ class TrashDeleteController extends ControllerBase { */ public function trashEntity() { $parameters = $this->routeMatch->getParameters()->all(); - $entities = array_filter($parameters, function ($parameter) { return $parameter instanceof ContentEntityInterface; }); $entity = reset($entities); + // Return the entity delete form for non-moderated entities. if (!$this->moderationInformation->isModeratedEntity($entity)) { return $this->entityFormBuilder->getForm($entity, 'delete'); } - $entity->get('moderation_state')->target_id = 'archived'; - if ($entity->save()) { + if ($this->trashManager->trash($entity)) { drupal_set_message($this->t('The @entity %label has been moved to the trash. <a href=":undo-page">Undo</a>', [ '@entity' => $entity->getEntityType() ->getLabel(), @@ -105,7 +116,6 @@ class TrashDeleteController extends ControllerBase { 'entity_id' => $entity->id(), ])->toString(), ])); - } return $this->redirect($this->getRedirectUrl($entity) diff --git a/src/TrashManager.php b/src/TrashManager.php index b2a18a3..729d25b 100644 --- a/src/TrashManager.php +++ b/src/TrashManager.php @@ -4,4 +4,12 @@ namespace Drupal\trash; class TrashManager implements TrashManagerInterface { + /** + * {@inheritdoc} + */ + public function trash($entity) { + $entity->set('moderation_state', 'archived'); + return (bool) $entity->save(); + } + } diff --git a/src/TrashManagerInterface.php b/src/TrashManagerInterface.php index e16ec7b..374651c 100644 --- a/src/TrashManagerInterface.php +++ b/src/TrashManagerInterface.php @@ -4,4 +4,11 @@ namespace Drupal\trash; interface TrashManagerInterface { -} \ No newline at end of file + /** + * @param \Drupal\Core\Entity\ContentEntityInterface $entity + * + * @return bool + */ + public function trash($entity); + +} -- GitLab