You are here

public function FlagSubscriber::onFlag in Open Social 8.5

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::onFlag()
  2. 8.6 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber::onFlag()
  3. 8.7 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber::onFlag()
  4. 8.8 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber::onFlag()
  5. 10.3.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber::onFlag()
  6. 10.0.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber::onFlag()
  7. 10.1.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber::onFlag()
  8. 10.2.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber::onFlag()

Listener for flagging events.

Parameters

\Drupal\flag\Event\FlaggingEvent $event: The event when something is flagged.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

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

Class

FlagSubscriber
Class FlagSubscriber.

Namespace

Drupal\social_content_report\EventSubscriber

Code

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,
    ]);
  }
}