public function ContentReportService::getModalLink in Open Social 8.8
Same name and namespace in other branches
- 8.9 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
- 8.5 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
- 8.6 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
- 8.7 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
- 10.3.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
- 10.0.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
- 10.1.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
- 10.2.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
Throws
\Drupal\Core\Entity\EntityMalformedException
Overrides ContentReportServiceInterface::getModalLink
File
- modules/
social_features/ social_content_report/ src/ ContentReportService.php, line 97
Class
- ContentReportService
- Provides a content report service.
Namespace
Drupal\social_content_reportCode
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',
],
],
];
}