You are here

public function ContentReportService::getModalLink in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  2. 8.5 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  3. 8.6 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  4. 8.7 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  5. 8.8 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  6. 10.0.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  7. 10.1.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  8. 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_report

Code

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 ($currentRequest !== NULL && $currentRequest
    ->isXmlHttpRequest() === TRUE) {
    if ($entity instanceof CommentInterface) {

      // 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();
      }
    }
    elseif ($currentRequest->headers
      ->has('referer')) {
      $currentUrl = $currentRequest->headers
        ->get('referer');
    }
  }

  /** @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',
      ],
    ],
  ];
}