You are here

public function MailingListController::subscribePage in Mailing List 8

Displays add subscription links for available mailing lists.

Redirects to mailing_list/subscription/add/[mailing_list] if only one mailing_list is available.

Return value

array|\Symfony\Component\HttpFoundation\RedirectResponse A render array for a list of the mailing lists that current user can add subscriptions. RedirectResponse to the subscription add page for the mailing list if only one if present.

1 string reference to 'MailingListController::subscribePage'
mailing_list.routing.yml in ./mailing_list.routing.yml
mailing_list.routing.yml

File

src/Controller/MailingListController.php, line 56

Class

MailingListController
Returns responses for Mailing list routes.

Namespace

Drupal\mailing_list\Controller

Code

public function subscribePage() {
  $entityTypeManager = $this
    ->entityTypeManager();
  $build = [
    '#theme' => 'subscription_add_list',
    '#cache' => [
      'tags' => $entityTypeManager
        ->getDefinition('mailing_list')
        ->getListCacheTags(),
    ],
  ];
  $content = [];

  // Only use mailing lists the user has access to.
  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;
    }
  }

  // Bypass the mailing lists listing if only one mailing list is available.
  if (count($content) == 1) {
    $bundle = array_shift($content);
    return $this
      ->redirect('mailing_list.subscribe', [
      'mailing_list' => $bundle
        ->id(),
    ]);
  }
  $build['#content'] = $content;
  return $build;
}