You are here

class ContentReportService in Open Social 8.9

Same name and namespace in other branches
  1. 8.5 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
  2. 8.6 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
  3. 8.7 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
  4. 8.8 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
  5. 10.3.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
  6. 10.0.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
  7. 10.1.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService
  8. 10.2.x modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService

Provides a content report service.

Hierarchy

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_report
View 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

Namesort descending Modifiers Type Description Overrides
ContentReportService::$currentUser protected property Current user.
ContentReportService::$flagService protected property Flag service.
ContentReportService::$moduleHandler protected property The module handler.
ContentReportService::$requestStack protected property The current request.
ContentReportService::getModalLink public function Overrides ContentReportServiceInterface::getModalLink
ContentReportService::getReportFlagTypes public function Gets all the 'report_' flag types. Overrides ContentReportServiceInterface::getReportFlagTypes
ContentReportService::__construct public function Constructor for ContentReportService.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.