class FlagSubscriber in Open Social 10.0.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
- 8.5 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
- 8.6 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
- 8.7 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
- 8.8 modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
- 10.3.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
- 10.1.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
- 10.2.x modules/social_features/social_content_report/src/EventSubscriber/FlagSubscriber.php \Drupal\social_content_report\EventSubscriber\FlagSubscriber
Class FlagSubscriber.
Hierarchy
- class \Drupal\social_content_report\EventSubscriber\FlagSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses StringTranslationTrait
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\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FlagSubscriber:: |
protected | property | The Cache tags invalidator service. | |
FlagSubscriber:: |
protected | property | The Messenger service. | |
FlagSubscriber:: |
protected | property | The content report service. | |
FlagSubscriber:: |
protected | property | Whether to unpublish the entity immediately on reporting or not. | |
FlagSubscriber:: |
public static | function | ||
FlagSubscriber:: |
public | function | Listener for flagging events. | |
FlagSubscriber:: |
public | function | Creates a DiffFormatter to render diffs in a table. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |