class ContentReportService in Open Social 8.9
Same name and namespace in other branches
- 8.5 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
- 8.6 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
- 8.7 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
- 8.8 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
- 10.3.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
- 10.0.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
- 10.1.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
- 10.2.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
Provides a content report service.
Hierarchy
- class \Drupal\social_content_report\ContentReportService implements ContentReportServiceInterface uses StringTranslationTrait
Expanded class hierarchy of ContentReportService
1 string reference to 'ContentReportService'
- 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 ContentReportService
- social_content_report.content_report_service in modules/
social_features/ social_content_report/ social_content_report.services.yml - Drupal\social_content_report\ContentReportService
File
- modules/
social_features/ social_content_report/ src/ ContentReportService.php, line 18
Namespace
Drupal\social_content_reportView source
class ContentReportService implements ContentReportServiceInterface {
use StringTranslationTrait;
/**
* Flag service.
*
* @var \Drupal\flag\FlagServiceInterface
*/
protected $flagService;
/**
* Current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructor for ContentReportService.
*
* @param \Drupal\flag\FlagServiceInterface $flag_service
* Flag service.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* Current user.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack.
*/
public function __construct(FlagServiceInterface $flag_service, AccountProxyInterface $current_user, ModuleHandlerInterface $module_handler, RequestStack $requestStack) {
$this->flagService = $flag_service;
$this->currentUser = $current_user;
$this->moduleHandler = $module_handler;
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public function getReportFlagTypes() : array {
$report_flags = $this->moduleHandler
->invokeAll('social_content_report_flags');
// Allow using reports for three predefined entity types.
$report_flags = array_merge($report_flags, [
'report_comment',
'report_node',
'report_post',
]);
$this->moduleHandler
->alter('social_content_report_flags', $report_flags);
return $report_flags;
}
/**
* {@inheritdoc}
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
public function getModalLink(EntityInterface $entity, $flag_id, $is_button = FALSE) : ?array {
// Check if users may flag this entity.
if (!$this->currentUser
->hasPermission('flag ' . $flag_id)) {
return NULL;
}
$flagging = FALSE;
$flag = $this->flagService
->getFlagById($flag_id);
if ($flag !== NULL) {
$flagging = $this->flagService
->getFlagging($flag, $entity, $this->currentUser);
}
// If the user already flagged this, we return a disabled link to nowhere.
if ($flagging) {
$element = [
'title' => $this
->t('Reported'),
'attributes' => [
'class' => [
'disabled',
],
],
];
if ($is_button) {
$element += [
'url' => Url::fromRoute('<none>'),
'attributes' => [
'class' => [
'btn',
'btn-link',
],
],
];
}
return $element;
}
// Return the modal link if the user did not yet flag this content.
$currentUrl = Url::fromRoute('<current>')
->toString();
$currentRequest = $this->requestStack
->getCurrentRequest();
// If there's a request and it's an ajax request, we need to do something
// different. Current url will now be determined based on something else.
if ($entity instanceof CommentInterface && $currentRequest !== NULL && $currentRequest
->isXmlHttpRequest() === TRUE) {
// Determine the parent entity, so we can redirect to the entity
// the comment was added to.
$parentEntity = $entity
->getCommentedEntity();
if ($parentEntity !== NULL) {
$currentUrl = $parentEntity
->toUrl()
->toString();
}
}
/** @var \Drupal\comment\Entity\Comment $entity */
return [
'title' => $this
->t('Report'),
'url' => Url::fromRoute('flag.field_entry', [
'flag' => $flag_id,
'entity_id' => $entity
->id(),
], [
'query' => [
'destination' => $currentUrl,
],
]),
'attributes' => [
'data-dialog-type' => 'modal',
'data-dialog-options' => JSON::encode([
'width' => 400,
'dialogClass' => 'content-reporting-dialog',
]),
'class' => [
'use-ajax',
'content-reporting-link',
],
],
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentReportService:: |
protected | property | Current user. | |
ContentReportService:: |
protected | property | Flag service. | |
ContentReportService:: |
protected | property | The module handler. | |
ContentReportService:: |
protected | property | The current request. | |
ContentReportService:: |
public | function |
Overrides ContentReportServiceInterface:: |
|
ContentReportService:: |
public | function |
Gets all the 'report_' flag types. Overrides ContentReportServiceInterface:: |
|
ContentReportService:: |
public | function | Constructor for ContentReportService. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
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. |