|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2loop_upvote\EventSubscriber; |
| 4 | + |
| 5 | +use Drupal\comment\Entity\Comment; |
| 6 | +use Drupal\flag\FlagServiceInterface; |
| 7 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 8 | +use Drupal\flag\Event\FlagEvents; |
| 9 | +use Drupal\flag\Event\FlaggingEvent; |
| 10 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Event subscriber for flagging content or terms with subscribe. |
| 14 | + */ |
| 15 | +class UpvoteFlagSubscriber implements EventSubscriberInterface { |
| 16 | + /** |
| 17 | + * The entity type manager. |
| 18 | + * |
| 19 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 20 | + */ |
| 21 | + protected $entityTypeManager; |
| 22 | + |
| 23 | + /** |
| 24 | + * The flag service. |
| 25 | + * |
| 26 | + * @var \Drupal\flag\FlagServiceInterface |
| 27 | + */ |
| 28 | + protected $flagService; |
| 29 | + |
| 30 | + /** |
| 31 | + * Helper constructor. |
| 32 | + * |
| 33 | + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
| 34 | + * Entity type manager. |
| 35 | + * @param \Drupal\flag\FlagServiceInterface $flagService |
| 36 | + * The flag service. |
| 37 | + * |
| 38 | + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
| 39 | + * Thrown if the entity type doesn't exist. |
| 40 | + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
| 41 | + * Thrown if the storage handler couldn't be loaded. |
| 42 | + */ |
| 43 | + public function __construct(EntityTypeManagerInterface $entityTypeManager, FlagServiceInterface $flagService) { |
| 44 | + $this->entityTypeManager = $entityTypeManager; |
| 45 | + $this->flagService = $flagService; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Event callback when an entity is flagged. |
| 50 | + * |
| 51 | + * @param \Drupal\flag\Event\FlaggingEvent $event |
| 52 | + * A flagging event. |
| 53 | + */ |
| 54 | + public function onFlag(FlaggingEvent $event) { |
| 55 | + $flag_type_id = $event->getFlagging()->getFlagId(); |
| 56 | + if ('os2loop_upvote_correct_answer' === $flag_type_id) { |
| 57 | + // The flagged entity. |
| 58 | + $entity = $event->getFlagging()->getFlaggable(); |
| 59 | + if ('comment' === $entity->getEntityTypeId()) { |
| 60 | + /** @var \Drupal\comment\CommentInterface $entity */ |
| 61 | + $node = $entity->getCommentedEntity(); |
| 62 | + $node_comments = $this->getNodeComments($node->id()); |
| 63 | + foreach ($node_comments as $comment) { |
| 64 | + if ($comment->id() !== $entity->id()) { |
| 65 | + // Unflag comment. |
| 66 | + $this->unflagComment($flag_type_id, $comment); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritdoc} |
| 75 | + */ |
| 76 | + public static function getSubscribedEvents() { |
| 77 | + $events = []; |
| 78 | + $events[FlagEvents::ENTITY_FLAGGED][] = ['onFlag']; |
| 79 | + return $events; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get all comments related to a node. |
| 84 | + * |
| 85 | + * @param string $nid |
| 86 | + * The Id of a node. |
| 87 | + * |
| 88 | + * @return array |
| 89 | + * A list of comments. |
| 90 | + */ |
| 91 | + private function getNodeComments(string $nid): array { |
| 92 | + try { |
| 93 | + $comment_ids = $this->entityTypeManager |
| 94 | + ->getStorage('comment') |
| 95 | + ->getQuery('AND') |
| 96 | + ->condition('entity_id', $nid) |
| 97 | + ->condition('entity_type', 'node') |
| 98 | + ->execute(); |
| 99 | + |
| 100 | + return $this->entityTypeManager |
| 101 | + ->getStorage('comment') |
| 102 | + ->loadMultiple($comment_ids); |
| 103 | + } |
| 104 | + |
| 105 | + catch (\Exception $exception) { |
| 106 | + return []; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Unflag a comment if a flag exists. |
| 112 | + * |
| 113 | + * @param string $flag_type_id |
| 114 | + * The flag type. |
| 115 | + * @param \Drupal\comment\Entity\Comment $comment |
| 116 | + * The comment to unflag. |
| 117 | + */ |
| 118 | + private function unflagComment(string $flag_type_id, Comment $comment) { |
| 119 | + $flag = $this->flagService->getFlagById($flag_type_id); |
| 120 | + if ($this->flagService->getFlagging($flag, $comment)) { |
| 121 | + $this->flagService->unflag($flag, $comment); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments