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]);
    }
  }
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]);
    }
  }
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 ] ) ; } }
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
Listener for flagging events .