SocialInviteSubscriber.php in Open Social 10.2.x
File
modules/social_features/social_core/src/EventSubscriber/SocialInviteSubscriber.php
View source
<?php
namespace Drupal\social_core\EventSubscriber;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\social_core\InviteService;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class SocialInviteSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
protected $inviteService;
protected $currentUser;
protected $messenger;
protected $currentRoute;
protected $alternativeFrontpageSettings;
protected $siteSettings;
public function __construct(InviteService $inviteService, AccountInterface $current_user, MessengerInterface $messenger, CurrentRouteMatch $route_match, ConfigFactory $config_factory) {
$this->inviteService = $inviteService;
$this->currentUser = $current_user;
$this->messenger = $messenger;
$this->currentRoute = $route_match;
$this->alternativeFrontpageSettings = $config_factory
->get('alternative_frontpage.settings');
$this->siteSettings = $config_factory
->get('system.site');
}
public function notifyAboutPendingInvitations(GetResponseEvent $event) {
$data = $this->inviteService
->getInviteData();
$request = $event
->getRequest();
$request_path = $request
->getPathInfo();
$route_name = $this->currentRoute
->getRouteName();
$default_front = $this->siteSettings
->get('page.front');
$frontpage_lu = $this->alternativeFrontpageSettings
->get('frontpage_for_authenticated_user') ?: '';
$paths_allowed = [
$default_front,
$frontpage_lu,
'/',
];
$routes_allowed = [
'social_core.homepage',
];
if (in_array($request_path, $paths_allowed) || in_array($route_name, $routes_allowed)) {
if (!empty($data['name']) && !empty($data['amount'])) {
$replacement_url = [
'@url' => Url::fromRoute($data['name'], [
'user' => $this->currentUser
->id(),
])
->toString(),
];
$message = $this
->formatPlural($data['amount'], 'You have 1 pending invite <a href="@url">visit your invite overview</a> to see it.', 'You have @count pending invites <a href="@url">visit your invite overview</a> to see them.', $replacement_url);
$this->messenger
->addMessage($message, 'warning');
}
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'notifyAboutPendingInvitations',
];
return $events;
}
}