You are here

class FlagSubscriber in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
  2. 8.5 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
  3. 8.6 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
  4. 8.7 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
  5. 8.8 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
  6. 10.3.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
  7. 10.1.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
  8. 10.2.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber

Class FlagSubscriber.

Hierarchy

Expanded class hierarchy of FlagSubscriber

1 string reference to 'FlagSubscriber'
social_content_report.services.yml in modules/social_features/social_content_report/social_content_report.services.yml
modules/social_features/social_content_report/social_content_report.services.yml
1 service uses FlagSubscriber
social_content_report.flag_subscriber in modules/social_features/social_content_report/social_content_report.services.yml
Drupal\social_content_report\EventSubscriber\FlagSubscriber

File

modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php, line 19

Namespace

Drupal\social_content_report\EventSubscriber
View source
class FlagSubscriber implements EventSubscriberInterface {
  use StringTranslationTrait, LoggerChannelTrait;

  /**
   * Whether to unpublish the entity immediately on reporting or not.
   *
   * @var bool
   */
  protected $unpublishImmediately;

  /**
   * The Messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The Cache tags invalidator service.
   *
   * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
   */
  protected $cacheInvalidator;

  /**
   * The content report service.
   *
   * @var \Drupal\social_content_report\ContentReportServiceInterface
   */
  protected $socialContentReport;

  /**
   * Creates a DiffFormatter to render diffs in a table.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_invalidator
   *   The cache tags invalidator service.
   * @param \Drupal\social_content_report\ContentReportServiceInterface $social_content_report
   *   The content report service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, MessengerInterface $messenger, CacheTagsInvalidatorInterface $cache_invalidator, ContentReportServiceInterface $social_content_report) {
    $this->unpublishImmediately = $config_factory
      ->get('social_content_report.settings')
      ->get('unpublish_threshold');
    $this->messenger = $messenger;
    $this->cacheInvalidator = $cache_invalidator;
    $this->socialContentReport = $social_content_report;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[FlagEvents::ENTITY_FLAGGED][] = [
      'onFlag',
    ];
    return $events;
  }

  /**
   * Listener for flagging events.
   *
   * @param \Drupal\flag\Event\FlaggingEvent $event
   *   The event when something is flagged.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function onFlag(FlaggingEvent $event) {
    $flagging = $event
      ->getFlagging();
    if (!in_array($flagging
      ->getFlagId(), $this->socialContentReport
      ->getReportFlagTypes())) {
      return;
    }

    // Retrieve the entity.
    $entity = $flagging
      ->getFlaggable();
    $entity_type = $entity
      ->getEntityTypeId();
    $entity_id = $entity
      ->id();
    $invalidated = FALSE;

    // Do nothing unless we need to unpublish the entity immediately.
    if ($this->unpublishImmediately) {
      try {
        $entity
          ->setPublished(FALSE);
        $entity
          ->save();
        $invalidated = TRUE;
      } catch (EntityStorageException $exception) {
        $this
          ->getLogger('social_content_report')
          ->error(t('@entity_type @entity_id could not be unpublished after a user reported it.', [
          '@entity_type' => $entity_type,
          '@entity_id' => $entity_id,
        ]));
      }
    }

    // In any case log that the report was submitted.
    $this->messenger
      ->addMessage($this
      ->t('Your report has been submitted.'));

    // Clear cache tags for entity to remove the Report link.
    if (!$invalidated) {
      $this->cacheInvalidator
        ->invalidateTags([
        $entity_type . ':' . $entity_id,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagSubscriber::$cacheInvalidator protected property The Cache tags invalidator service.
FlagSubscriber::$messenger protected property The Messenger service.
FlagSubscriber::$socialContentReport protected property The content report service.
FlagSubscriber::$unpublishImmediately protected property Whether to unpublish the entity immediately on reporting or not.
FlagSubscriber::getSubscribedEvents public static function
FlagSubscriber::onFlag public function Listener for flagging events.
FlagSubscriber::__construct public function Creates a DiffFormatter to render diffs in a table.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.