You are here

function simplenews_user_view in Simplenews 8.2

Same name and namespace in other branches
  1. 8 simplenews.module \simplenews_user_view()
  2. 7.2 simplenews.module \simplenews_user_view()
  3. 7 simplenews.module \simplenews_user_view()
  4. 3.x simplenews.module \simplenews_user_view()

Implements hook_user_view().

File

./simplenews.module, line 498
Simplenews node handling, sent email, newsletter block and general hooks.

Code

function simplenews_user_view(array &$build, UserInterface $account, EntityViewDisplayInterface $display, $view_mode) {
  $user = \Drupal::currentUser();
  $build['#cache']['contexts'][] = 'user.permissions';
  if ($user
    ->id() == $account
    ->id() || $user
    ->hasPermission('administer users')) {
    if ($display
      ->getComponent('simplenews')) {
      $build['simplenews'] = [
        '#type' => 'details',
        '#title' => t('Subscribed to'),
        '#open' => TRUE,
      ];

      // Collect newsletter to which the current user is subscribed.
      // 'hidden' newsletters are not listed.
      $links = [];
      if ($subscriber = Subscriber::loadByUid($account
        ->id())) {
        foreach (simplenews_newsletter_get_visible() as $newsletter) {
          if ($subscriber
            ->isSubscribed($newsletter
            ->id())) {

            // @todo Make links
            $links[] = $newsletter
              ->label();
          }
        }
      }

      // When a user has no permission to subscribe and is not subscribed
      // we do not display the 'no subscriptions' message.
      if ($account
        ->hasPermission('subscribe to newsletters')) {
        if ($links) {
          $build['simplenews']['subscriptions'] = [
            '#theme' => 'item_list',
            '#items' => $links,
          ];
        }
        else {
          $build['simplenews']['subscriptions'] = [
            '#type' => 'item',
            '#markup' => t('None'),
          ];
        }
      }
      if ($account
        ->hasPermission('subscribe to newsletters')) {
        $build['simplenews']['my_newsletters'] = [
          '#type' => 'link',
          '#title' => t('Manage subscriptions'),
          '#url' => new Url('simplenews.newsletter_subscriptions_user', [
            'user' => $account
              ->id(),
          ]),
        ];
      }
    }
  }
}