diff --git a/flag.services.yml b/flag.services.yml index 17eaa31..52348df 100644 --- a/flag.services.yml +++ b/flag.services.yml @@ -8,8 +8,6 @@ services: flag: class: Drupal\flag\FlagService arguments: ['@plugin.manager.flag.flagtype', '@event_dispatcher', '@entity.query', '@current_user', '@entity.manager'] - tags: - - { name: event_subscriber } flag.count: class: Drupal\flag\FlagCountManager arguments: ['@database'] @@ -18,3 +16,8 @@ services: flag.link_builder: class: Drupal\flag\FlagLinkBuilder arguments: ['@entity.manager', '@flag'] + flag.flagging_delete: + class: Drupal\flag\FlaggingDeletionService + arguments: ['@entity.query', '@event_dispatcher', '@flag'] + tags: + - { name: event_subscriber } diff --git a/src/FlagService.php b/src/FlagService.php index 63b8429..171ddec 100644 --- a/src/FlagService.php +++ b/src/FlagService.php @@ -12,22 +12,18 @@ use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Session\AccountInterface; use Drupal\flag\Event\FlagEvents; -use Drupal\flag\Event\FlagResetEvent; use Drupal\flag\Event\FlaggingEvent; -use Drupal\flag\Event\FlagDeleteEvent; use Drupal\flag\FlagInterface; use Drupal\flag\FlagServiceInterface; use Drupal\flag\FlagTypePluginManager; use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Flag service. * - Handles search requests for flags and flaggings. * - Performs flagging and unflaging operations. - * - Resets a flag on request and automatically when a flag is deleted. */ -class FlagService implements FlagServiceInterface, EventSubscriberInterface{ +class FlagService implements FlagServiceInterface { /** * The flag type plugin manager injected into the service. @@ -272,31 +268,6 @@ class FlagService implements FlagServiceInterface, EventSubscriberInterface{ } /** - * {@inheritdoc} - */ - public function reset(FlagInterface $flag, EntityInterface $entity = NULL) { - $query = $this->entityQueryManager->get('flagging') - ->condition('flag_id', $flag->id()); - - if (!empty($entity)) { - $query->condition('entity_id', $entity->id()); - } - - // Count the number of flaggings to delete. - $count = $query->count() - ->execute(); - - $this->eventDispatcher->dispatch(FlagEvents::FLAG_RESET, new FlagResetEvent($flag, $count)); - - $flaggings = $this->getFlaggings($flag, $entity); - foreach ($flaggings as $flagging) { - $flagging->delete(); - } - - return $count; - } - - /** * Loads flag entities given their IDs. * * @param int[] $ids @@ -322,23 +293,4 @@ class FlagService implements FlagServiceInterface, EventSubscriberInterface{ return $this->entityManager->getStorage('flagging')->loadMultiple($ids); } - /** - * Responds to flag delete events and removes all the assocated flaggings. - * - * @param FlagDeleteEvent $event - * The Flag Event. - */ - public function onDeleteResetFlaggings(FlagDeleteEvent $event){ - $this->reset($event->getFlag()); - } - - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() { - $events = array(); - $events[FlagEvents::FLAG_DELETED][] = array('onDeleteResetFlaggings', -100); - return $events; - } - } diff --git a/src/FlagServiceInterface.php b/src/FlagServiceInterface.php index ff437a1..22a758c 100644 --- a/src/FlagServiceInterface.php +++ b/src/FlagServiceInterface.php @@ -147,18 +147,5 @@ interface FlagServiceInterface { */ public function unflag(FlagInterface $flag, EntityInterface $entity, AccountInterface $account = NULL); - /** - * - * Remove all flagged entities from a flag. - * - * @param FlagInterface $flag - * The flag to reset. - * @param EntityInterface $entity - * (optional) The entity for which to delete flaggings. - * - * @return int - * The number of flaggings that have been deleted. - */ - public function reset(FlagInterface $flag, EntityInterface $entity = NULL); } diff --git a/src/FlaggingDeletionService.php b/src/FlaggingDeletionService.php new file mode 100644 index 0000000..d4f643e --- /dev/null +++ b/src/FlaggingDeletionService.php @@ -0,0 +1,117 @@ +entityQueryManager = $entity_query; + $this->eventDispatcher = $event_dispatcher; + $this->flagService = $flag_service; + } + + /** + * {@inheritdoc} + */ + public function reset(FlagInterface $flag, EntityInterface $entity = NULL) { + $query = $this->entityQueryManager->get('flagging') + ->condition('flag_id', $flag->id()); + + if (!empty($entity)) { + $query->condition('entity_id', $entity->id()); + } + + // Count the number of flaggings to delete. + $count = $query->count() + ->execute(); + + $this->eventDispatcher->dispatch(FlagEvents::FLAG_RESET, new FlagResetEvent($flag, $count)); + + $flaggings = $this->flagService->getFlaggings($flag, $entity); + foreach ($flaggings as $flagging) { + $flagging->delete(); + } + + return $count; + } + + /** + * Responds to flag delete events and removes all the assocated flaggings. + * + * @param FlagDeleteEvent $event + * The Flag Event. + */ + public function onDeleteResetFlaggings(FlagDeleteEvent $event) { + $this->reset($event->getFlag()); + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events = array(); + $events[FlagEvents::FLAG_DELETED][] = array('onDeleteResetFlaggings', -100); + return $events; + } + +} diff --git a/src/FlaggingDeletionServiceInterface.php b/src/FlaggingDeletionServiceInterface.php new file mode 100644 index 0000000..9029836 --- /dev/null +++ b/src/FlaggingDeletionServiceInterface.php @@ -0,0 +1,33 @@ +flagService = $flag_service; + public function __construct(FlaggingDeletionServiceInterface $flag_delete) { + $this->flagDelete = $flag_delete; } /** @@ -43,7 +45,7 @@ class FlagResetForm extends ConfirmFormBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('flag') + $container->get('flag.flagging_delete:') ); } @@ -100,7 +102,7 @@ class FlagResetForm extends ConfirmFormBase { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $this->flagService->reset($this->flag); + $this->flagDelete->reset($this->flag); drupal_set_message($this->t('Flag %label was reset.', [ '%label' => $this->flag->label(), ]));