MailingListController.php in Mailing List 8
File
src/Controller/MailingListController.php
View source
<?php
namespace Drupal\mailing_list\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\mailing_list\SubscriptionInterface;
use Drupal\mailing_list\MailingListManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Drupal\mailing_list\MailingListInterface;
class MailingListController extends ControllerBase implements ContainerInjectionInterface {
protected $mailingListManager;
public function __construct(MailingListManagerInterface $mailing_list_manager) {
$this->mailingListManager = $mailing_list_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('mailing_list.manager'));
}
public function subscribePage() {
$entityTypeManager = $this
->entityTypeManager();
$build = [
'#theme' => 'subscription_add_list',
'#cache' => [
'tags' => $entityTypeManager
->getDefinition('mailing_list')
->getListCacheTags(),
],
];
$content = [];
foreach ($entityTypeManager
->getStorage('mailing_list')
->loadMultiple() as $bundle) {
$access = $entityTypeManager
->getAccessControlHandler('mailing_list_subscription')
->createAccess($bundle
->id(), NULL, [], TRUE);
if ($access
->isAllowed()) {
$content[$bundle
->id()] = $bundle;
}
}
if (count($content) == 1) {
$bundle = array_shift($content);
return $this
->redirect('mailing_list.subscribe', [
'mailing_list' => $bundle
->id(),
]);
}
$build['#content'] = $content;
return $build;
}
public function subscribeForm(MailingListInterface $mailing_list) {
$subscription = $this
->entityTypeManager()
->getStorage('mailing_list_subscription')
->create([
'mailing_list' => $mailing_list
->id(),
]);
return $this
->entityFormBuilder()
->getForm($subscription);
}
public function accessSubscription($sid, $hash, $rel = 'canonical') {
$subscription_storage = $this
->entityTypeManager()
->getStorage('mailing_list_subscription');
if (!($subscription = $subscription_storage
->load($sid))) {
throw new NotFoundHttpException();
}
$url = $subscription
->toUrl($rel);
if (!($go = $url
->access())) {
if ($subscription
->getAccessHash() == $hash && ($subscription
->getList()
->isCrossAccessAllowed() || $subscription
->getOwnerId() == $this
->currentUser()
->id())) {
$this->mailingListManager
->grantSessionAccess($subscription);
$go = TRUE;
}
}
if ($go) {
if ($this
->currentUser()
->isAnonymous()) {
foreach ($subscription_storage
->loadMultiple($subscription_storage
->getQuery()
->condition('uid', 0)
->condition('email', $subscription
->getEmail())
->condition('status', SubscriptionInterface::ACTIVE)
->execute()) as $additional_subscription) {
if (!$additional_subscription
->access('view')) {
$this->mailingListManager
->grantSessionAccess($additional_subscription);
}
}
}
return $this
->redirect($url
->getRouteName(), $url
->getRouteParameters());
}
else {
throw new AccessDeniedHttpException();
}
}
}