You are here

public function ContentReportService::getModalLink in Open Social 8.6

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.7 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  4. 8.8 modules/social_features/social_content_report/src/ContentReportService.php \Drupal\social_content_report\ContentReportService::getModalLink()
  5. 10.3.x 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()

Returns a modal link to the reporting form to use in a #links array.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to create the report for.

string $flag_id: The flag ID.

bool $is_button: TRUE if need to show it like button.

Return value

array|null A renderable array to be used in a #links array or FALSE if the user has no access.

Overrides ContentReportServiceInterface::getModalLink

File

modules/social_features/social_content_report/src/ContentReportService.php, line 82

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;
  }
  $flag = $this->flagService
    ->getFlagById($flag_id);
  $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.
  return [
    'title' => $this
      ->t('Report'),
    'url' => Url::fromRoute('flag.field_entry', [
      'flag' => $flag_id,
      'entity_id' => $entity
        ->id(),
    ], [
      'query' => [
        'destination' => Url::fromRoute('<current>')
          ->toString(),
      ],
    ]),
    'attributes' => [
      'data-dialog-type' => 'modal',
      'data-dialog-options' => JSON::encode([
        'width' => 400,
        'dialogClass' => 'content-reporting-dialog',
      ]),
      'class' => [
        'use-ajax',
        'content-reporting-link',
      ],
    ],
  ];
}